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
+11 -16
View File
@@ -74,22 +74,18 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
swarm_info = json.loads(json_str)
if swarm_info["Swarm"]["NodeID"]:
return True
if swarm_info["Swarm"]["LocalNodeState"] in (
return swarm_info["Swarm"]["LocalNodeState"] in (
"active",
"pending",
"locked",
):
return True
)
return False
else:
try:
node_info = self.get_node_inspect(node_id=node_id)
except APIError:
return
try:
node_info = self.get_node_inspect(node_id=node_id)
except APIError:
return
if node_info["ID"] is not None:
return True
return False
return node_info["ID"] is not None
def check_if_swarm_manager(self):
"""
@@ -138,8 +134,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
True if node is part of swarm but its state is down, False otherwise
"""
if repeat_check < 1:
repeat_check = 1
repeat_check = max(1, repeat_check)
if node_id is None:
node_id = self.get_swarm_node_id()
@@ -179,7 +174,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
if skip_missing:
return None
self.fail(f"Error while reading from Swarm manager: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting swarm node: {exc}")
json_str = json.dumps(node_info, ensure_ascii=False)
@@ -215,7 +210,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
"Cannot inspect node: To inspect node execute module on Swarm Manager"
)
self.fail(f"Error while reading from Swarm manager: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting swarm node: {exc}")
json_str = json.dumps(node_info, ensure_ascii=False)
@@ -295,7 +290,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
"Cannot inspect service: To inspect service execute module on Swarm Manager"
)
self.fail(f"Error inspecting swarm service: {exc}")
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting swarm service: {exc}")
json_str = json.dumps(service_info, ensure_ascii=False)