From 5b5f7e9e64ed956250059225f6906b1113345f6b Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Wed, 31 Dec 2025 04:15:24 +1300 Subject: [PATCH] batch 1 - update Python idiom to 3.7 using pyupgrade (#11341) * batch 1 - update Python idiom to 3.7 using pyupgrade * add changelog frag * add changelog frag --- changelogs/fragments/11341-pyupgrade-1.yml | 19 +++++++++++++++++++ plugins/cache/yaml.py | 2 +- plugins/callback/logentries.py | 2 +- plugins/callback/timestamp.py | 2 +- plugins/connection/chroot.py | 2 +- plugins/connection/jail.py | 2 +- plugins/connection/lxc.py | 10 +++++----- plugins/connection/wsl.py | 2 +- plugins/connection/zone.py | 2 +- plugins/inventory/cobbler.py | 4 ++-- plugins/inventory/linode.py | 2 +- plugins/inventory/lxd.py | 6 +++--- plugins/inventory/opennebula.py | 4 ++-- plugins/module_utils/alicloud_ecs.py | 2 +- plugins/module_utils/known_hosts.py | 2 +- plugins/module_utils/lxd.py | 2 +- plugins/module_utils/manageiq.py | 4 ++-- plugins/module_utils/oracle/oci_utils.py | 2 +- plugins/module_utils/univention_umc.py | 6 +++--- 19 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 changelogs/fragments/11341-pyupgrade-1.yml diff --git a/changelogs/fragments/11341-pyupgrade-1.yml b/changelogs/fragments/11341-pyupgrade-1.yml new file mode 100644 index 0000000000..a4a63ab524 --- /dev/null +++ b/changelogs/fragments/11341-pyupgrade-1.yml @@ -0,0 +1,19 @@ +minor_changes: + - yaml cache plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - logentries callback plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - timestamp callback plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - chroot connection plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - jail connection plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - lxc connection plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - wsl connection plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - zone connection plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - cobbler inventory plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - linode inventory plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - lxd inventory plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - opennebula inventory plugin - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - alicloud_ecs module utils - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - known_hosts module utils - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - lxd module utils - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - manageiq module utils - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - oci_utils module utils - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). + - univention_umc module utils - update to Python 3.7 idioms (https://github.com/ansible-collections/community.general/pull/11341). diff --git a/plugins/cache/yaml.py b/plugins/cache/yaml.py index f91f3fe0b4..cfcb2111bf 100644 --- a/plugins/cache/yaml.py +++ b/plugins/cache/yaml.py @@ -58,7 +58,7 @@ class CacheModule(BaseFileCacheModule): """ def _load(self, filepath): - with open(os.path.abspath(filepath), "r", encoding="utf-8") as f: + with open(os.path.abspath(filepath), encoding="utf-8") as f: return AnsibleLoader(f).get_single_data() def _dump(self, value, filepath): diff --git a/plugins/callback/logentries.py b/plugins/callback/logentries.py index ede0a2416e..769512c17b 100644 --- a/plugins/callback/logentries.py +++ b/plugins/callback/logentries.py @@ -178,7 +178,7 @@ class PlainTextSocketAppender: while True: try: self._conn.send(to_bytes(multiline, errors="surrogate_or_strict")) - except socket.error: + except OSError: self.reopen_connection() continue break diff --git a/plugins/callback/timestamp.py b/plugins/callback/timestamp.py index b4f75a20fd..ba4e48f0de 100644 --- a/plugins/callback/timestamp.py +++ b/plugins/callback/timestamp.py @@ -89,7 +89,7 @@ def banner(self, msg, color=None, cows=True): msg = msg.strip() try: star_len = self.columns - get_text_width(msg) - timestamp_len - except EnvironmentError: + except OSError: star_len = self.columns - len(msg) - timestamp_len if star_len <= 3: star_len = 3 diff --git a/plugins/connection/chroot.py b/plugins/connection/chroot.py index 0812b37fd1..4dfb30a703 100644 --- a/plugins/connection/chroot.py +++ b/plugins/connection/chroot.py @@ -200,7 +200,7 @@ class Connection(ConnectionBase): raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e if p.returncode != 0: raise AnsibleError(f"failed to transfer file {in_path} to {out_path}:\n{stdout}\n{stderr}") - except IOError as e: + except OSError as e: raise AnsibleError(f"file or module does not exist at: {in_path}") from e def fetch_file(self, in_path, out_path): diff --git a/plugins/connection/jail.py b/plugins/connection/jail.py index cf48f26324..5a22e964b6 100644 --- a/plugins/connection/jail.py +++ b/plugins/connection/jail.py @@ -172,7 +172,7 @@ class Connection(ConnectionBase): raise AnsibleError( f"failed to transfer file {in_path} to {out_path}:\n{to_native(stdout)}\n{to_native(stderr)}" ) - except IOError as e: + except OSError as e: raise AnsibleError(f"file or module does not exist at: {in_path}") from e def fetch_file(self, in_path, out_path): diff --git a/plugins/connection/lxc.py b/plugins/connection/lxc.py index 81e3a1ac8a..8bd6e65363 100644 --- a/plugins/connection/lxc.py +++ b/plugins/connection/lxc.py @@ -94,7 +94,7 @@ class Connection(ConnectionBase): while len(read_fds) > 0 or len(write_fds) > 0: try: ready_reads, ready_writes, dummy = select.select(read_fds, write_fds, []) - except select.error as e: + except OSError as e: if e.args[0] == errno.EINTR: continue raise @@ -173,7 +173,7 @@ class Connection(ConnectionBase): raise errors.AnsibleFileNotFound(msg) try: src_file = open(in_path, "rb") - except IOError as e: + except OSError as e: traceback.print_exc() raise errors.AnsibleError(f"failed to open input file to {in_path}") from e try: @@ -184,7 +184,7 @@ class Connection(ConnectionBase): try: self.container.attach_wait(write_file, None) - except IOError as e: + except OSError as e: traceback.print_exc() msg = f"failed to transfer file to {out_path}" raise errors.AnsibleError(msg) from e @@ -200,7 +200,7 @@ class Connection(ConnectionBase): try: dst_file = open(out_path, "wb") - except IOError as e: + except OSError as e: traceback.print_exc() msg = f"failed to open output file {out_path}" raise errors.AnsibleError(msg) from e @@ -217,7 +217,7 @@ class Connection(ConnectionBase): try: self.container.attach_wait(write_file, None) - except IOError as e: + except OSError as e: traceback.print_exc() msg = f"failed to transfer file from {in_path} to {out_path}" raise errors.AnsibleError(msg) from e diff --git a/plugins/connection/wsl.py b/plugins/connection/wsl.py index 9e792b22ea..67fa7e4fc9 100644 --- a/plugins/connection/wsl.py +++ b/plugins/connection/wsl.py @@ -475,7 +475,7 @@ class Connection(ConnectionBase): try: ssh.load_system_host_keys(ssh_known_hosts) break - except IOError: + except OSError: pass # file was not found, but not required to function except paramiko.hostkeys.InvalidHostKey as e: raise AnsibleConnectionFailure(f"Invalid host key: {to_text(e.line)}") from e diff --git a/plugins/connection/zone.py b/plugins/connection/zone.py index fcb5c33616..e631f1f9dc 100644 --- a/plugins/connection/zone.py +++ b/plugins/connection/zone.py @@ -169,7 +169,7 @@ class Connection(ConnectionBase): raise AnsibleError(f"failed to transfer file {in_path} to {out_path}") from e if p.returncode != 0: raise AnsibleError(f"failed to transfer file {in_path} to {out_path}:\n{stdout}\n{stderr}") - except IOError as e: + except OSError as e: raise AnsibleError(f"file or module does not exist at: {in_path}") from e def fetch_file(self, in_path, out_path): diff --git a/plugins/inventory/cobbler.py b/plugins/inventory/cobbler.py index 55b6d4afbf..65e5ed943a 100644 --- a/plugins/inventory/cobbler.py +++ b/plugins/inventory/cobbler.py @@ -197,7 +197,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): data = self.cobbler.get_profiles(self.token) else: data = self.cobbler.get_profiles() - except (socket.gaierror, socket.error, xmlrpc_client.ProtocolError): + except (socket.gaierror, OSError, xmlrpc_client.ProtocolError): self._reload_cache() else: self._init_cache() @@ -221,7 +221,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): data[i] = self.cobbler.get_system_as_rendered(host["name"], self.token) else: data[i] = self.cobbler.get_system_as_rendered(host["name"]) - except (socket.gaierror, socket.error, xmlrpc_client.ProtocolError): + except (socket.gaierror, OSError, xmlrpc_client.ProtocolError): self._reload_cache() else: self._init_cache() diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py index 9538496b38..bec4525a6d 100644 --- a/plugins/inventory/linode.py +++ b/plugins/inventory/linode.py @@ -203,7 +203,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): ips += [instance.ips.ipv6.slaac, instance.ips.ipv6.link_local] ips += instance.ips.ipv6.pools - for ip_type in set(ip.type for ip in ips): + for ip_type in {ip.type for ip in ips}: self.inventory.set_variable( hostname, ip_type, make_unsafe(self._ip_data([ip for ip in ips if ip.type == ip_type])) ) diff --git a/plugins/inventory/lxd.py b/plugins/inventory/lxd.py index 0b85036a74..603c68fb3d 100644 --- a/plugins/inventory/lxd.py +++ b/plugins/inventory/lxd.py @@ -210,9 +210,9 @@ class InventoryModule(BaseInventoryPlugin): Returns: dict(json_data): json data""" try: - with open(path, "r") as json_file: + with open(path) as json_file: return json.load(json_file) - except (IOError, json.decoder.JSONDecodeError) as err: + except (OSError, json.decoder.JSONDecodeError) as err: raise AnsibleParserError(f"Could not load the test data from {to_native(path)}: {err}") from err def save_json_data(self, path, file_name=None): @@ -242,7 +242,7 @@ class InventoryModule(BaseInventoryPlugin): cwd = os.path.abspath(os.path.dirname(__file__)) with open(os.path.abspath(os.path.join(cwd, *path)), "w") as json_file: json.dump(self.data, json_file) - except IOError as err: + except OSError as err: raise AnsibleParserError(f"Could not save data: {err}") from err def verify_file(self, path): diff --git a/plugins/inventory/opennebula.py b/plugins/inventory/opennebula.py index 176f1ba0ff..c9224752b0 100644 --- a/plugins/inventory/opennebula.py +++ b/plugins/inventory/opennebula.py @@ -119,10 +119,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable): if authfile is None: authfile = os.path.join(os.environ.get("HOME"), ".one", "one_auth") try: - with open(authfile, "r") as fp: + with open(authfile) as fp: authstring = fp.read().rstrip() username, password = authstring.split(":") - except (OSError, IOError) as e: + except OSError as e: raise AnsibleError(f"Could not find or read ONE_AUTH file at '{authfile}'") from e except Exception as e: raise AnsibleError(f"Error occurs when reading ONE_AUTH file at '{authfile}'") from e diff --git a/plugins/module_utils/alicloud_ecs.py b/plugins/module_utils/alicloud_ecs.py index fb9c1bfb9d..071ccce469 100644 --- a/plugins/module_utils/alicloud_ecs.py +++ b/plugins/module_utils/alicloud_ecs.py @@ -163,7 +163,7 @@ def get_profile(params): else f"{os.getenv('HOME')}/.aliyun/config.json" ) auth = {} - with open(path, "r") as f: + with open(path) as f: for pro in json.load(f)["profiles"]: if params["profile"] == pro["name"]: auth = pro diff --git a/plugins/module_utils/known_hosts.py b/plugins/module_utils/known_hosts.py index 7626fea03e..1a30b3649c 100644 --- a/plugins/module_utils/known_hosts.py +++ b/plugins/module_utils/known_hosts.py @@ -97,7 +97,7 @@ def not_in_host_file(self, host): try: with open(hf) as host_fh: data = host_fh.read() - except IOError: + except OSError: hfiles_not_found += 1 continue diff --git a/plugins/module_utils/lxd.py b/plugins/module_utils/lxd.py index 7513acd69e..a217da1fea 100644 --- a/plugins/module_utils/lxd.py +++ b/plugins/module_utils/lxd.py @@ -115,7 +115,7 @@ class LXDClient: return resp_json self._raise_err_from_json(resp_json) return resp_json - except socket.error as e: + except OSError as e: raise LXDClientException("cannot connect to the LXD server", err=e) from e def _raise_err_from_json(self, resp_json): diff --git a/plugins/module_utils/manageiq.py b/plugins/module_utils/manageiq.py index 3936079f21..122e6c0807 100644 --- a/plugins/module_utils/manageiq.py +++ b/plugins/module_utils/manageiq.py @@ -292,7 +292,7 @@ class ManageIQPolicies: # make a list of assigned full profile names strings # e.g. ['openscap profile', ...] - assigned_profiles_set = set(profile["profile_name"] for profile in assigned_profiles) + assigned_profiles_set = {profile["profile_name"] for profile in assigned_profiles} for profile in profiles: assigned = profile.get("name") in assigned_profiles_set @@ -398,7 +398,7 @@ class ManageIQTags: # make a list of assigned full tag names strings # e.g. ['/managed/environment/prod', ...] - assigned_tags_set = set(tag["full_name"] for tag in assigned_tags) + assigned_tags_set = {tag["full_name"] for tag in assigned_tags} for tag in tags: assigned = self.full_tag_name(tag) in assigned_tags_set diff --git a/plugins/module_utils/oracle/oci_utils.py b/plugins/module_utils/oracle/oci_utils.py index 4281afb0f2..63e9a2df32 100644 --- a/plugins/module_utils/oracle/oci_utils.py +++ b/plugins/module_utils/oracle/oci_utils.py @@ -1352,7 +1352,7 @@ def delete_and_wait( :return: A dictionary containing the resource & the "changed" status. e.g. {"vcn":{x:y}, "changed":True} """ - states_set = set(["DETACHING", "DETACHED", "DELETING", "DELETED", "TERMINATING", "TERMINATED"]) + states_set = {"DETACHING", "DETACHED", "DELETING", "DELETED", "TERMINATING", "TERMINATED"} result: dict[str, t.Any] = dict(changed=False) result[resource_type] = dict() try: diff --git a/plugins/module_utils/univention_umc.py b/plugins/module_utils/univention_umc.py index 887e7a6492..7e4c922d47 100644 --- a/plugins/module_utils/univention_umc.py +++ b/plugins/module_utils/univention_umc.py @@ -88,10 +88,10 @@ def uldap(): def construct(): try: - secret_file = open("/etc/ldap.secret", "r") + secret_file = open("/etc/ldap.secret") bind_dn = f"cn=admin,{base_dn()}" - except IOError: # pragma: no cover - secret_file = open("/etc/machine.secret", "r") + except OSError: # pragma: no cover + secret_file = open("/etc/machine.secret") bind_dn = config_registry()["ldap/hostdn"] pwd_line = secret_file.readline() pwd = re.sub("\n", "", pwd_line)