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
@@ -134,6 +134,7 @@ class ContainerManager(DockerBaseClass):
"The value of default_host_ip must be an empty string, an IPv4 address, "
f'or an IPv6 address. Got "{self.param_default_host_ip}" instead.'
)
self.parameters = None
def _collect_all_options(self, active_options):
all_options = {}
@@ -480,7 +481,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.unpause_container(
self.client, container.id
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error {'pausing' if self.param_paused else 'unpausing'} container {container.id}: {exc}"
)
@@ -567,13 +568,13 @@ class ContainerManager(DockerBaseClass):
if not image or self.param_pull == "always":
if not self.check_mode:
self.log("Pull the image.")
image, alreadyToLatest = self.engine_driver.pull_image(
image, already_to_latest = self.engine_driver.pull_image(
self.client,
repository,
tag,
image_platform=self.module.params["platform"],
)
if alreadyToLatest:
if already_to_latest:
self.results["changed"] = False
self.results["actions"].append(
dict(pulled_image=f"{repository}:{tag}", changed=False)
@@ -950,7 +951,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.disconnect_container_from_network(
self.client, container.id, diff["parameter"]["id"]
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error disconnecting container from network {diff['parameter']['name']} - {exc}"
)
@@ -975,7 +976,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.connect_container_to_network(
self.client, container.id, diff["parameter"]["id"], params
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error connecting container to network {diff['parameter']['name']} - {exc}"
)
@@ -989,7 +990,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.disconnect_container_from_network(
self.client, container.id, network["name"]
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Error disconnecting container from network {network['name']} - {exc}"
)
@@ -1027,7 +1028,7 @@ class ContainerManager(DockerBaseClass):
container_id = self.engine_driver.create_container(
self.client, self.param_name, create_parameters, networks=networks
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error creating container: {exc}")
return self._get_container(container_id)
return new_container
@@ -1039,7 +1040,7 @@ class ContainerManager(DockerBaseClass):
if not self.check_mode:
try:
self.engine_driver.start_container(self.client, container_id)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error starting container {container_id}: {exc}")
if self.module.params["detach"] is False:
@@ -1096,7 +1097,7 @@ class ContainerManager(DockerBaseClass):
link=link,
force=force,
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.client.fail(f"Error removing container {container_id}: {exc}")
def container_update(self, container_id, update_parameters):
@@ -1112,7 +1113,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.update_container(
self.client, container_id, update_parameters
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error updating container {container_id}: {exc}")
return self._get_container(container_id)
@@ -1126,7 +1127,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.kill_container(
self.client, container_id, kill_signal=self.param_kill_signal
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error killing container {container_id}: {exc}")
def container_restart(self, container_id):
@@ -1139,7 +1140,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.restart_container(
self.client, container_id, self.module.params["stop_timeout"] or 10
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error restarting container {container_id}: {exc}")
return self._get_container(container_id)
@@ -1156,7 +1157,7 @@ class ContainerManager(DockerBaseClass):
self.engine_driver.stop_container(
self.client, container_id, self.module.params["stop_timeout"]
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error stopping container {container_id}: {exc}")