From 2bb49b4f8277d2c15cf8e867ee972e2b6385a56b Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Mon, 12 Aug 2024 16:35:00 -0600 Subject: [PATCH] chore(mypy): Fix failures on newer versions of mypy (#5607) --- cloudinit/config/schema.py | 4 +--- cloudinit/distros/aosc.py | 2 +- cloudinit/net/__init__.py | 12 +++++------- cloudinit/net/netops/__init__.py | 5 +++++ cloudinit/net/openbsd.py | 2 +- cloudinit/safeyaml.py | 4 ++-- cloudinit/url_helper.py | 3 +-- pyproject.toml | 1 + tests/unittests/config/test_cc_write_files.py | 5 +++-- tests/unittests/helpers.py | 7 ++++++- tests/unittests/test_net.py | 10 ++++------ 11 files changed, 30 insertions(+), 25 deletions(-) diff --git a/cloudinit/config/schema.py b/cloudinit/config/schema.py index a2fceecabcb..f34db025457 100644 --- a/cloudinit/config/schema.py +++ b/cloudinit/config/schema.py @@ -46,9 +46,7 @@ ) try: - from jsonschema import ValidationError as _ValidationError - - ValidationError = _ValidationError + from jsonschema import ValidationError except ImportError: ValidationError = Exception # type: ignore diff --git a/cloudinit/distros/aosc.py b/cloudinit/distros/aosc.py index 0460c740d5c..5f126136a02 100644 --- a/cloudinit/distros/aosc.py +++ b/cloudinit/distros/aosc.py @@ -107,7 +107,7 @@ def package_command(self, command, args=None, pkgs=None): def install_packages(self, pkglist: PackageList): self.package_command("install", pkgs=pkglist) - def update_package_sources(self): + def update_package_sources(self, *, force=False): self._runner.run( "update-sources", self.package_command, diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index e38b6779dd4..6a213f78865 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -555,10 +555,8 @@ def find_fallback_nic_on_linux() -> Optional[str]: return None -def generate_fallback_config(config_driver=None): +def generate_fallback_config(config_driver=None) -> Optional[dict]: """Generate network cfg v2 for dhcp on the NIC most likely connected.""" - if not config_driver: - config_driver = False target_name = find_fallback_nic() if not target_name: @@ -572,16 +570,16 @@ def generate_fallback_config(config_driver=None): match = { "macaddress": read_sys_net_safe(target_name, "address").lower() } + if config_driver: + driver = device_driver(target_name) + if driver: + match["driver"] = driver cfg = { "dhcp4": True, "dhcp6": True, "set-name": target_name, "match": match, } - if config_driver: - driver = device_driver(target_name) - if driver: - cfg["match"]["driver"] = driver nconf = {"ethernets": {target_name: cfg}, "version": 2} return nconf diff --git a/cloudinit/net/netops/__init__.py b/cloudinit/net/netops/__init__.py index 7b95917874b..8c5e2bd13c6 100644 --- a/cloudinit/net/netops/__init__.py +++ b/cloudinit/net/netops/__init__.py @@ -1,3 +1,4 @@ +from abc import abstractmethod from typing import Optional from cloudinit.subp import SubpResult @@ -5,14 +6,17 @@ class NetOps: @staticmethod + @abstractmethod def link_up(interface: str) -> SubpResult: pass @staticmethod + @abstractmethod def link_down(interface: str) -> SubpResult: pass @staticmethod + @abstractmethod def link_rename(current_name: str, new_name: str): pass @@ -41,6 +45,7 @@ def del_route( pass @staticmethod + @abstractmethod def get_default_route() -> str: pass diff --git a/cloudinit/net/openbsd.py b/cloudinit/net/openbsd.py index 83b33e0380c..6dea579b141 100644 --- a/cloudinit/net/openbsd.py +++ b/cloudinit/net/openbsd.py @@ -10,7 +10,7 @@ class Renderer(cloudinit.net.bsd.BSDRenderer): - def write_config(self): + def write_config(self, target=None): for device_name, v in self.interface_configurations.items(): if_file = "etc/hostname.{}".format(device_name) fn = subp.target_path(self.target, if_file) diff --git a/cloudinit/safeyaml.py b/cloudinit/safeyaml.py index 2d4612e9647..5ee626bda06 100644 --- a/cloudinit/safeyaml.py +++ b/cloudinit/safeyaml.py @@ -162,8 +162,8 @@ def _get_nested_path_prefix(self, node): return f"{mark.path}." return "" - def construct_mapping(self, node): - mapping = super().construct_mapping(node) + def construct_mapping(self, node, deep=False): + mapping = super().construct_mapping(node, deep=deep) nested_path_prefix = self._get_nested_path_prefix(node) for key_node, value_node in node.value: node_key_path = f"{nested_path_prefix}{key_node.value}" diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py index 9cb3d4a0088..f2c6eb2e634 100644 --- a/cloudinit/url_helper.py +++ b/cloudinit/url_helper.py @@ -293,8 +293,7 @@ def __init__(self, response: requests.Response): @property def contents(self) -> bytes: if self._response.content is None: - # typeshed bug: https://github.com/python/typeshed/pull/12180 - return b"" # type: ignore + return b"" return self._response.content @property diff --git a/pyproject.toml b/pyproject.toml index da98e92b39c..2adba376194 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ module = [ "paramiko.*", "pip.*", "pycloudlib.*", + "responses", "serial", "tests.integration_tests.user_settings", "uaclient.*", diff --git a/tests/unittests/config/test_cc_write_files.py b/tests/unittests/config/test_cc_write_files.py index ec0024971ad..7f7f1740112 100644 --- a/tests/unittests/config/test_cc_write_files.py +++ b/tests/unittests/config/test_cc_write_files.py @@ -138,13 +138,14 @@ def test_all_decodings(self): b64 = (base64.b64encode(data), b64_aliases) for content, aliases in (gz, gz_b64, b64): for enc in aliases: + path = "/tmp/file-%s-%s" % (name, enc) cur = { "content": content, - "path": "/tmp/file-%s-%s" % (name, enc), + "path": path, "encoding": enc, } files.append(cur) - expected.append((cur["path"], data)) + expected.append((path, data)) write_files("test_decoding", files, self.owner) diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py index ab97973e02a..b214170c3ef 100644 --- a/tests/unittests/helpers.py +++ b/tests/unittests/helpers.py @@ -599,11 +599,16 @@ def skipIfAptPkg(): try: + import importlib.metadata + import jsonschema assert jsonschema # avoid pyflakes error F401: import unused _jsonschema_version = tuple( - int(part) for part in jsonschema.__version__.split(".") # type: ignore + int(part) + for part in importlib.metadata.metadata("jsonschema") + .get("Version", "") + .split(".") ) _missing_jsonschema_dep = False except ImportError: diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index dbae4f20267..08bf5aa64de 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -3551,14 +3551,12 @@ def test_render( ) if network_cfg is None: - network_cfg = net.generate_fallback_config() + parsed_cfg = net.generate_fallback_config() else: - network_cfg = yaml.safe_load(network_cfg) - assert isinstance(network_cfg, dict) + parsed_cfg = yaml.safe_load(network_cfg) + assert isinstance(parsed_cfg, dict) - ns = network_state.parse_net_config_data( - network_cfg, skip_broken=False - ) + ns = network_state.parse_net_config_data(parsed_cfg, skip_broken=False) render_dir = os.path.join(tmp_dir, "render") os.makedirs(render_dir)