From 8a0feb1ec04b211f9444e0eeaf473b670db475bd Mon Sep 17 00:00:00 2001 From: Alberto Contreras Date: Tue, 14 Mar 2023 10:36:36 +0100 Subject: chore: fix style tip (#2071) - remove too broad exceptions - ignore dynamic base types in templater --- cloudinit/cmd/devel/hotplug_hook.py | 2 +- cloudinit/config/cc_disk_setup.py | 38 ++++++++++++---------- cloudinit/config/cc_lxd.py | 4 +-- cloudinit/config/cc_reset_rmc.py | 2 +- cloudinit/net/__init__.py | 2 +- cloudinit/net/networkd.py | 2 +- cloudinit/sources/DataSourceDigitalOcean.py | 2 +- cloudinit/sources/DataSourceHetzner.py | 2 +- cloudinit/sources/DataSourceNWCS.py | 4 +-- cloudinit/sources/DataSourceUpCloud.py | 4 ++- cloudinit/sources/helpers/vmware/imc/config_nic.py | 2 +- cloudinit/templater.py | 9 +++-- cloudinit/user_data.py | 2 +- tests/integration_tests/clouds.py | 2 +- tests/integration_tests/conftest.py | 2 +- tests/integration_tests/instances.py | 4 +-- tests/unittests/config/test_cc_disk_setup.py | 2 +- tests/unittests/config/test_cc_growpart.py | 2 +- .../unittests/config/test_cc_power_state_change.py | 2 +- tests/unittests/config/test_cc_set_hostname.py | 2 +- tests/unittests/helpers.py | 2 +- tests/unittests/sources/test_init.py | 5 ++- tests/unittests/test_cli.py | 8 ++--- tests/unittests/test_features.py | 2 +- tests/unittests/test_netinfo.py | 4 +-- tests/unittests/test_util.py | 2 +- 26 files changed, 59 insertions(+), 55 deletions(-) diff --git a/cloudinit/cmd/devel/hotplug_hook.py b/cloudinit/cmd/devel/hotplug_hook.py index 560857ef..78085735 100755 --- a/cloudinit/cmd/devel/hotplug_hook.py +++ b/cloudinit/cmd/devel/hotplug_hook.py @@ -168,7 +168,7 @@ def is_enabled(hotplug_init, subsystem): try: scope = SUBSYSTEM_PROPERTES_MAP[subsystem][1] except KeyError as e: - raise Exception( + raise RuntimeError( "hotplug-hook: cannot handle events for subsystem: {}".format( subsystem ) diff --git a/cloudinit/config/cc_disk_setup.py b/cloudinit/config/cc_disk_setup.py index 4aae5530..ecaca079 100644 --- a/cloudinit/config/cc_disk_setup.py +++ b/cloudinit/config/cc_disk_setup.py @@ -274,7 +274,7 @@ def enumerate_disk(device, nodeps=False): try: info, _err = subp.subp(lsblk_cmd) except Exception as e: - raise Exception( + raise RuntimeError( "Failed during disk check for %s\n%s" % (device, e) ) from e @@ -338,7 +338,7 @@ def check_fs(device): try: out, _err = subp.subp(blkid_cmd, rcs=[0, 2]) except Exception as e: - raise Exception( + raise RuntimeError( "Failed during disk check for %s\n%s" % (device, e) ) from e @@ -444,7 +444,7 @@ def get_hdd_size(device): size_in_bytes, _ = subp.subp([BLKDEV_CMD, "--getsize64", device]) sector_size, _ = subp.subp([BLKDEV_CMD, "--getss", device]) except Exception as e: - raise Exception("Failed to get %s size\n%s" % (device, e)) from e + raise RuntimeError("Failed to get %s size\n%s" % (device, e)) from e return int(size_in_bytes) / int(sector_size) @@ -462,7 +462,7 @@ def check_partition_mbr_layout(device, layout): try: out, _err = subp.subp(prt_cmd, data="%s\n" % layout) except Exception as e: - raise Exception( + raise RuntimeError( "Error running partition command on %s\n%s" % (device, e) ) from e @@ -493,7 +493,7 @@ def check_partition_gpt_layout(device, layout): try: out, _err = subp.subp(prt_cmd, update_env=LANG_C_ENV) except Exception as e: - raise Exception( + raise RuntimeError( "Error running partition command on %s\n%s" % (device, e) ) from e @@ -542,7 +542,7 @@ def check_partition_layout(table_type, device, layout): elif "mbr" == table_type: found_layout = check_partition_mbr_layout(device, layout) else: - raise Exception("Unable to determine table type") + raise RuntimeError("Unable to determine table type") LOG.debug( "called check_partition_%s_layout(%s, %s), returned: %s", @@ -595,11 +595,11 @@ def get_partition_mbr_layout(size, layout): if (len(layout) == 0 and isinstance(layout, list)) or not isinstance( layout, list ): - raise Exception("Partition layout is invalid") + raise RuntimeError("Partition layout is invalid") last_part_num = len(layout) if last_part_num > 4: - raise Exception("Only simply partitioning is allowed.") + raise RuntimeError("Only simply partitioning is allowed.") part_definition = [] part_num = 0 @@ -610,7 +610,9 @@ def get_partition_mbr_layout(size, layout): if isinstance(part, list): if len(part) != 2: - raise Exception("Partition was incorrectly defined: %s" % part) + raise RuntimeError( + "Partition was incorrectly defined: %s" % part + ) percent, part_type = part part_size = int(float(size) * (float(percent) / 100)) @@ -622,7 +624,7 @@ def get_partition_mbr_layout(size, layout): sfdisk_definition = "\n".join(part_definition) if len(part_definition) > 4: - raise Exception( + raise RuntimeError( "Calculated partition definition is too big\n%s" % sfdisk_definition ) @@ -638,7 +640,7 @@ def get_partition_gpt_layout(size, layout): for partition in layout: if isinstance(partition, list): if len(partition) != 2: - raise Exception( + raise RuntimeError( "Partition was incorrectly defined: %s" % partition ) percent, partition_type = partition @@ -682,7 +684,7 @@ def purge_disk(device): LOG.info("Purging filesystem on /dev/%s", d["name"]) subp.subp(wipefs_cmd) except Exception as e: - raise Exception( + raise RuntimeError( "Failed FS purge of /dev/%s" % d["name"] ) from e @@ -702,7 +704,7 @@ def get_partition_layout(table_type, size, layout): return get_partition_mbr_layout(size, layout) elif "gpt" == table_type: return get_partition_gpt_layout(size, layout) - raise Exception("Unable to determine table type") + raise RuntimeError("Unable to determine table type") def read_parttbl(device): @@ -733,7 +735,7 @@ def exec_mkpart_mbr(device, layout): try: subp.subp(prt_cmd, data="%s\n" % layout) except Exception as e: - raise Exception( + raise RuntimeError( "Failed to partition device %s\n%s" % (device, e) ) from e @@ -816,7 +818,7 @@ def mkpart(device, definition): # This prevents you from overwriting the device LOG.debug("Checking if device %s is a valid device", device) if not is_device_valid(device): - raise Exception( + raise RuntimeError( "Device {device} is not a disk device!".format(device=device) ) @@ -849,7 +851,7 @@ def mkpart(device, definition): elif "gpt" == table_type: exec_mkpart_gpt(device, part_definition) else: - raise Exception("Unable to determine table type") + raise RuntimeError("Unable to determine table type") LOG.debug("Partition table created for %s", device) @@ -997,7 +999,7 @@ def mkfs(fs_cfg): # Check that we can create the FS if not (fs_type or fs_cmd): - raise Exception( + raise RuntimeError( "No way to create filesystem '{label}'. fs_type or fs_cmd " "must be set.".format(label=label) ) @@ -1059,7 +1061,7 @@ def mkfs(fs_cfg): try: subp.subp(fs_cmd, shell=shell) except Exception as e: - raise Exception("Failed to exec of '%s':\n%s" % (fs_cmd, e)) from e + raise RuntimeError("Failed to exec of '%s':\n%s" % (fs_cmd, e)) from e # vi: ts=4 expandtab diff --git a/cloudinit/config/cc_lxd.py b/cloudinit/config/cc_lxd.py index e692fbd5..06c9f6a6 100644 --- a/cloudinit/config/cc_lxd.py +++ b/cloudinit/config/cc_lxd.py @@ -382,7 +382,7 @@ def bridge_to_debconf(bridge_cfg): debconf["lxd/bridge-domain"] = bridge_cfg.get("domain") else: - raise Exception('invalid bridge mode "%s"' % bridge_cfg.get("mode")) + raise RuntimeError('invalid bridge mode "%s"' % bridge_cfg.get("mode")) return debconf @@ -399,7 +399,7 @@ def bridge_to_cmd(bridge_cfg): return None, cmd_attach if bridge_cfg.get("mode") != "new": - raise Exception('invalid bridge mode "%s"' % bridge_cfg.get("mode")) + raise RuntimeError('invalid bridge mode "%s"' % bridge_cfg.get("mode")) cmd_create = ["network", "create", bridge_name] diff --git a/cloudinit/config/cc_reset_rmc.py b/cloudinit/config/cc_reset_rmc.py index a780e4ff..d687c482 100644 --- a/cloudinit/config/cc_reset_rmc.py +++ b/cloudinit/config/cc_reset_rmc.py @@ -149,4 +149,4 @@ def reset_rmc(): if node_id_after == node_id_before: msg = "New node ID did not get generated." LOG.error(msg) - raise Exception(msg) + raise RuntimeError(msg) diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 50e445ec..244305d1 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -882,7 +882,7 @@ def _rename_interfaces( ) if len(errors): - raise Exception("\n".join(errors)) + raise RuntimeError("\n".join(errors)) def get_interface_mac(ifname): diff --git a/cloudinit/net/networkd.py b/cloudinit/net/networkd.py index 6a75f38a..0c9ece0c 100644 --- a/cloudinit/net/networkd.py +++ b/cloudinit/net/networkd.py @@ -346,7 +346,7 @@ class Renderer(renderer.Renderer): f" and dhcp{version}-overrides.use-domains" f" configured. Use one" ) - raise Exception(exception) + raise RuntimeError(exception) self.parse_dhcp_overrides(cfg, device, dhcp, version) diff --git a/cloudinit/sources/DataSourceDigitalOcean.py b/cloudinit/sources/DataSourceDigitalOcean.py index 52d3ad26..b6a110aa 100644 --- a/cloudinit/sources/DataSourceDigitalOcean.py +++ b/cloudinit/sources/DataSourceDigitalOcean.py @@ -102,7 +102,7 @@ class DataSourceDigitalOcean(sources.DataSource): interfaces = self.metadata.get("interfaces") LOG.debug(interfaces) if not interfaces: - raise Exception("Unable to get meta-data from server....") + raise RuntimeError("Unable to get meta-data from server....") nameservers = self.metadata_full["dns"]["nameservers"] self._network_config = do_helper.convert_network_configuration( diff --git a/cloudinit/sources/DataSourceHetzner.py b/cloudinit/sources/DataSourceHetzner.py index 90531769..14f14677 100644 --- a/cloudinit/sources/DataSourceHetzner.py +++ b/cloudinit/sources/DataSourceHetzner.py @@ -129,7 +129,7 @@ class DataSourceHetzner(sources.DataSource): _net_config = self.metadata["network-config"] if not _net_config: - raise Exception("Unable to get meta-data from server....") + raise RuntimeError("Unable to get meta-data from server....") self._network_config = _net_config diff --git a/cloudinit/sources/DataSourceNWCS.py b/cloudinit/sources/DataSourceNWCS.py index e21383d2..3a483049 100644 --- a/cloudinit/sources/DataSourceNWCS.py +++ b/cloudinit/sources/DataSourceNWCS.py @@ -55,7 +55,7 @@ class DataSourceNWCS(sources.DataSource): md = self.get_metadata() if md is None: - raise Exception("failed to get metadata") + raise RuntimeError("failed to get metadata") self.metadata_full = md @@ -111,7 +111,7 @@ class DataSourceNWCS(sources.DataSource): return self._network_config if not self.metadata["network"]["config"]: - raise Exception("Unable to get metadata from server") + raise RuntimeError("Unable to get metadata from server") # metadata sends interface names, but we dont want to use them for i in self.metadata["network"]["config"]: diff --git a/cloudinit/sources/DataSourceUpCloud.py b/cloudinit/sources/DataSourceUpCloud.py index d6b74bc1..43122f0b 100644 --- a/cloudinit/sources/DataSourceUpCloud.py +++ b/cloudinit/sources/DataSourceUpCloud.py @@ -126,7 +126,9 @@ class DataSourceUpCloud(sources.DataSource): raw_network_config = self.metadata.get("network") if not raw_network_config: - raise Exception("Unable to get network meta-data from server....") + raise RuntimeError( + "Unable to get network meta-data from server...." + ) self._network_config = uc_helper.convert_network_config( raw_network_config, diff --git a/cloudinit/sources/helpers/vmware/imc/config_nic.py b/cloudinit/sources/helpers/vmware/imc/config_nic.py index 5ac8cbf1..ba2488be 100644 --- a/cloudinit/sources/helpers/vmware/imc/config_nic.py +++ b/cloudinit/sources/helpers/vmware/imc/config_nic.py @@ -62,7 +62,7 @@ class NicConfigurator: if not primary_nics: return None elif len(primary_nics) > 1: - raise Exception( + raise RuntimeError( "There can only be one primary nic", [nic.mac for nic in primary_nics], ) diff --git a/cloudinit/templater.py b/cloudinit/templater.py index 8f98bb5d..ed6b9063 100644 --- a/cloudinit/templater.py +++ b/cloudinit/templater.py @@ -15,14 +15,17 @@ import collections import re import sys -from typing import Type +from typing import Any from cloudinit import log as logging from cloudinit import type_utils as tu from cloudinit import util from cloudinit.atomic_helper import write_file -JUndefined: Type +# After bionic EOL, mypy==1.0.0 will be able to type-analyse dynamic +# base types, substitute this by: +# JUndefined: typing.Type +JUndefined: Any try: from jinja2 import DebugUndefined as _DebugUndefined from jinja2 import Template as JTemplate @@ -41,7 +44,7 @@ MISSING_JINJA_PREFIX = "CI_MISSING_JINJA_VAR/" # Mypy, and the PEP 484 ecosystem in general, does not support creating # classes with dynamic base types: https://stackoverflow.com/a/59636248 -class UndefinedJinjaVariable(JUndefined): # type: ignore +class UndefinedJinjaVariable(JUndefined): """Class used to represent any undefined jinja template variable.""" def __str__(self): diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py index 3336b23d..5374ec8a 100644 --- a/cloudinit/user_data.py +++ b/cloudinit/user_data.py @@ -69,7 +69,7 @@ def _set_filename(msg, filename): def _handle_error(error_message, source_exception=None): if features.ERROR_ON_USER_DATA_FAILURE: - raise Exception(error_message) from source_exception + raise RuntimeError(error_message) from source_exception else: LOG.warning(error_message) diff --git a/tests/integration_tests/clouds.py b/tests/integration_tests/clouds.py index 308ffedd..945a5fb6 100644 --- a/tests/integration_tests/clouds.py +++ b/tests/integration_tests/clouds.py @@ -402,7 +402,7 @@ class OpenstackCloud(IntegrationCloud): try: UUID(image.image_id) except ValueError as e: - raise Exception( + raise RuntimeError( "When using Openstack, `OS_IMAGE` MUST be specified with " "a 36-character UUID image ID. Passing in a release name is " "not valid here.\n" diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 782ca7e5..fabeb608 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -65,7 +65,7 @@ def pytest_runtest_setup(item): unsupported_message = "Cannot run on platform {}".format(current_platform) if "no_container" in test_marks: if "lxd_container" in test_marks: - raise Exception( + raise RuntimeError( "lxd_container and no_container marks simultaneously set " "on test" ) diff --git a/tests/integration_tests/instances.py b/tests/integration_tests/instances.py index cf2bf4cc..7bdf05d0 100644 --- a/tests/integration_tests/instances.py +++ b/tests/integration_tests/instances.py @@ -75,7 +75,7 @@ class IntegrationInstance: def execute(self, command, *, use_sudo=True) -> Result: if self.instance.username == "root" and use_sudo is False: - raise Exception("Root user cannot run unprivileged") + raise RuntimeError("Root user cannot run unprivileged") return self.instance.execute(command, use_sudo=use_sudo) def pull_file(self, remote_path, local_path): @@ -139,7 +139,7 @@ class IntegrationInstance: elif source == CloudInitSource.UPGRADE: self.upgrade_cloud_init() else: - raise Exception( + raise RuntimeError( "Specified to install {} which isn't supported here".format( source ) diff --git a/tests/unittests/config/test_cc_disk_setup.py b/tests/unittests/config/test_cc_disk_setup.py index 496ad8e1..39314313 100644 --- a/tests/unittests/config/test_cc_disk_setup.py +++ b/tests/unittests/config/test_cc_disk_setup.py @@ -75,7 +75,7 @@ class TestGetMbrHddSize(TestCase): return hdd_size_in_bytes, None elif "--getss" in cmd: return sector_size_in_bytes, None - raise Exception("Unexpected blockdev command called") + raise RuntimeError("Unexpected blockdev command called") self.subp.side_effect = _subp diff --git a/tests/unittests/config/test_cc_growpart.py b/tests/unittests/config/test_cc_growpart.py index 13622332..e4480341 100644 --- a/tests/unittests/config/test_cc_growpart.py +++ b/tests/unittests/config/test_cc_growpart.py @@ -381,7 +381,7 @@ class TestEncrypted: return "/dev/vdz" elif value.startswith("/dev"): return value - raise Exception(f"unexpected value {value}") + raise RuntimeError(f"unexpected value {value}") def _realpath_side_effect(self, value): return "/dev/dm-1" if value.startswith("/dev/mapper") else value diff --git a/tests/unittests/config/test_cc_power_state_change.py b/tests/unittests/config/test_cc_power_state_change.py index fbdc06ef..ccec0fde 100644 --- a/tests/unittests/config/test_cc_power_state_change.py +++ b/tests/unittests/config/test_cc_power_state_change.py @@ -164,7 +164,7 @@ def check_lps_ret(psc_return, mode=None): if len(errs): lines = ["Errors in result: %s" % str(psc_return)] + errs - raise Exception("\n".join(lines)) + raise RuntimeError("\n".join(lines)) class TestPowerStateChangeSchema: diff --git a/tests/unittests/config/test_cc_set_hostname.py b/tests/unittests/config/test_cc_set_hostname.py index 2c92949f..4e74c55c 100644 --- a/tests/unittests/config/test_cc_set_hostname.py +++ b/tests/unittests/config/test_cc_set_hostname.py @@ -226,7 +226,7 @@ class TestHostname(t_help.FilesystemMockingTestCase): distro = self._fetch_distro("debian") def set_hostname_error(hostname, fqdn): - raise Exception("OOPS on: %s" % fqdn) + raise RuntimeError("OOPS on: %s" % fqdn) distro.set_hostname = set_hostname_error paths = helpers.Paths({"cloud_dir": self.tmp}) diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py index fec63809..32503fb8 100644 --- a/tests/unittests/helpers.py +++ b/tests/unittests/helpers.py @@ -173,7 +173,7 @@ class CiTestCase(TestCase): ) if pass_through: return _real_subp(*args, **kwargs) - raise Exception( + raise RuntimeError( "called subp. set self.allowed_subp=True to allow\n subp(%s)" % ", ".join( [str(repr(a)) for a in args] diff --git a/tests/unittests/sources/test_init.py b/tests/unittests/sources/test_init.py index 0447e02c..96e4dd90 100644 --- a/tests/unittests/sources/test_init.py +++ b/tests/unittests/sources/test_init.py @@ -894,15 +894,14 @@ class TestDataSource(CiTestCase): self.datasource.default_update_events, ) - def fake_get_data(): - raise Exception("get_data should not be called") - + fake_get_data = mock.Mock() self.datasource.get_data = fake_get_data self.assertFalse( self.datasource.update_metadata_if_supported( source_event_types=[EventType.BOOT] ) ) + self.assertEqual([], fake_get_data.call_args_list) @mock.patch.dict( DataSource.supported_update_events, diff --git a/tests/unittests/test_cli.py b/tests/unittests/test_cli.py index 07294214..e3fed410 100644 --- a/tests/unittests/test_cli.py +++ b/tests/unittests/test_cli.py @@ -55,14 +55,12 @@ class TestCLI: data_d = tmpdir.join("data") link_d = tmpdir.join("link") FakeArgs = namedtuple("FakeArgs", ["action", "local", "mode"]) + my_action = mock.Mock() - def myaction(): - raise Exception("Should not call myaction") - - myargs = FakeArgs((action, myaction), False, "bogusmode") + myargs = FakeArgs((action, my_action), False, "bogusmode") with pytest.raises(ValueError, match=match): cli.status_wrapper(name, myargs, data_d, link_d) - assert "Should not call myaction" not in caplog.text + assert [] == my_action.call_args_list def test_status_wrapper_init_local_writes_fresh_status_info(self, tmpdir): """When running in init-local mode, status_wrapper writes status.json. diff --git a/tests/unittests/test_features.py b/tests/unittests/test_features.py index 94c7ae13..8aace78d 100644 --- a/tests/unittests/test_features.py +++ b/tests/unittests/test_features.py @@ -27,7 +27,7 @@ def create_override(request): """ override_path = Path(cloudinit.__file__).parent / "feature_overrides.py" if override_path.exists(): - raise Exception( + raise RuntimeError( "feature_overrides.py unexpectedly exists! " "Remove it to run this test." ) diff --git a/tests/unittests/test_netinfo.py b/tests/unittests/test_netinfo.py index aecce921..7612a28b 100644 --- a/tests/unittests/test_netinfo.py +++ b/tests/unittests/test_netinfo.py @@ -198,7 +198,7 @@ class TestNetInfo: return (SAMPLE_ROUTE_OUT_V4, "") if args[0] == ["netstat", "-A", "inet6", "--route", "--numeric"]: return (SAMPLE_ROUTE_OUT_V6, "") - raise Exception("Unexpected subp call %s" % args[0]) + raise RuntimeError("Unexpected subp call %s" % args[0]) m_subp.side_effect = subp_netstat_route_selector m_which.side_effect = lambda x: x if x == "netstat" else None @@ -216,7 +216,7 @@ class TestNetInfo: v6cmd = ["ip", "--oneline", "-6", "route", "list", "table", "all"] if v6cmd == args[0]: return (SAMPLE_IPROUTE_OUT_V6, "") - raise Exception("Unexpected subp call %s" % args[0]) + raise RuntimeError("Unexpected subp call %s" % args[0]) m_subp.side_effect = subp_iproute_selector m_which.side_effect = lambda x: x if x == "ip" else None diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 07142a86..ecf8cf5c 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -2877,7 +2877,7 @@ class TestFindDevs: return msdos elif pattern == "/dev/iso9660/*": return iso9660 - raise Exception + raise RuntimeError m_glob.side_effect = fake_glob -- cgit v1.2.1