Python code modernization, 4/n (#1162)

* Address attribute-defined-outside-init.

* Address broad-exception-raised.

* Address broad-exception-caught.

* Address consider-iterating-dictionary.

* Address consider-using-dict-comprehension.

* Address consider-using-f-string.

* Address consider-using-in.

* Address consider-using-max-builtin.

* Address some consider-using-with.

* Address invalid-name.

* Address keyword-arg-before-vararg.

* Address line-too-long.

* Address no-else-continue.

* Address no-else-raise.

* Address no-else-return.

* Remove broken dead code.

* Make consider-using-f-string changes compatible with older Python versions.

* Python 3.11 and earlier apparently do not like multi-line f-strings.
This commit is contained in:
Felix Fontein
2025-10-11 23:06:50 +02:00
committed by GitHub
parent 33c8a49191
commit cad22de628
59 changed files with 556 additions and 630 deletions
@@ -124,7 +124,7 @@ def _get_ansible_type(our_type):
if our_type == "set":
return "list"
if our_type not in ("list", "dict", "bool", "int", "float", "str"):
raise Exception(f'Invalid type "{our_type}"')
raise ValueError(f'Invalid type "{our_type}"')
return our_type
@@ -266,7 +266,7 @@ class DockerAPIEngineDriver(EngineDriver):
# Ensure driver_opts values are strings
for key, val in value.items():
if not isinstance(val, str):
raise Exception(
raise ValueError(
f"driver_opts values must be strings, got {type(val).__name__} for key '{key}'"
)
params[dest_para] = value
@@ -278,7 +278,7 @@ class DockerAPIEngineDriver(EngineDriver):
params[dest_para] = value
if parameters:
ups = ", ".join([f'"{p}"' for p in sorted(parameters)])
raise Exception(
raise ValueError(
f"Unknown parameter(s) for connect_container_to_network for Docker API driver: {ups}"
)
ipam_config = {}
@@ -347,8 +347,7 @@ class DockerAPIEngineDriver(EngineDriver):
)
output = client._get_result_tty(False, res, config["Config"]["Tty"])
return output, True
else:
return f"Result logged using `{logging_driver}` driver", False
return f"Result logged using `{logging_driver}` driver", False
def update_container(self, client, container_id, update_parameters):
result = client.post_json_to_json(
@@ -399,13 +398,13 @@ class DockerAPIEngineDriver(EngineDriver):
# New docker daemon versions do not allow containers to be removed
# if they are paused. Make sure we do not end up in an infinite loop.
if count == 3:
raise Exception(f"{exc} [tried to unpause three times]")
raise RuntimeError(f"{exc} [tried to unpause three times]")
count += 1
# Unpause
try:
self.unpause_container(client, container_id)
except Exception as exc2:
raise Exception(f"{exc2} [while unpausing]")
raise RuntimeError(f"{exc2} [while unpausing]")
# Now try again
continue
raise
@@ -430,13 +429,13 @@ class DockerAPIEngineDriver(EngineDriver):
# New docker daemon versions do not allow containers to be removed
# if they are paused. Make sure we do not end up in an infinite loop.
if count == 3:
raise Exception(f"{exc} [tried to unpause three times]")
raise RuntimeError(f"{exc} [tried to unpause three times]")
count += 1
# Unpause
try:
self.unpause_container(client, container_id)
except Exception as exc2:
raise Exception(f"{exc2} [while unpausing]")
raise RuntimeError(f"{exc2} [while unpausing]")
# Now try again
continue
if (
@@ -1060,7 +1059,7 @@ def _get_network_id(module, client, network_name):
network_id = network["Id"]
break
return network_id
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
client.fail(f"Error getting network id for {network_name} - {exc}")