From 35ef2af0ce1ae82638421e836a88889de47623f2 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 2 Mar 2022 11:52:51 -0800 Subject: [PATCH] Fix type hints and variable usage in modules. (#77184) * Remove unused variables. * Fix conflicting variable name. * Add type hints. --- lib/ansible/modules/hostname.py | 5 +++-- lib/ansible/modules/pip.py | 4 +++- lib/ansible/modules/user.py | 6 +++--- lib/ansible/modules/wait_for.py | 2 +- lib/ansible/modules/yum.py | 12 ++++++------ 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/hostname.py b/lib/ansible/modules/hostname.py index 4fefbe5769d..94f9dc35913 100644 --- a/lib/ansible/modules/hostname.py +++ b/lib/ansible/modules/hostname.py @@ -70,11 +70,12 @@ import os import platform import socket import traceback +import types try: import typing as t except ImportError: - t = None + t = None # type: types.ModuleType | None from ansible.module_utils.basic import ( AnsibleModule, @@ -657,7 +658,7 @@ class SLESHostname(Hostname): distribution_version = get_distribution_version() # cast to float may raise ValueError on non SLES, we use float for a little more safety over int if distribution_version and 10 <= float(distribution_version) <= 12: - strategy_class = SLESStrategy + strategy_class = SLESStrategy # type: t.Type[BaseStrategy] else: raise ValueError() except ValueError: diff --git a/lib/ansible/modules/pip.py b/lib/ansible/modules/pip.py index ee9aaadd5dc..8814ae7869b 100644 --- a/lib/ansible/modules/pip.py +++ b/lib/ansible/modules/pip.py @@ -272,6 +272,8 @@ import tempfile import operator import shlex import traceback +import types + from ansible.module_utils.compat.version import LooseVersion SETUPTOOLS_IMP_ERR = None @@ -456,7 +458,7 @@ def _have_pip_module(): # type: () -> bool try: import importlib except ImportError: - importlib = None + importlib = None # type: types.ModuleType | None if importlib: # noinspection PyBroadException diff --git a/lib/ansible/modules/user.py b/lib/ansible/modules/user.py index 2da70f75d2a..346d91e618d 100644 --- a/lib/ansible/modules/user.py +++ b/lib/ansible/modules/user.py @@ -499,7 +499,7 @@ class User(object): platform = 'Generic' distribution = None # type: str | None PASSWORDFILE = '/etc/passwd' - SHADOWFILE = '/etc/shadow' + SHADOWFILE = '/etc/shadow' # type: str | None SHADOWFILE_EXPIRE_INDEX = 7 LOGIN_DEFS = '/etc/login.defs' DATE_FORMAT = '%Y-%m-%d' @@ -1167,11 +1167,11 @@ class User(object): out_buffer = b'' err_buffer = b'' while p.poll() is None: - r, w, e = select.select([master_out_fd, master_err_fd], [], [], 1) + r_list = select.select([master_out_fd, master_err_fd], [], [], 1)[0] first_prompt = b'Enter passphrase (empty for no passphrase):' second_prompt = b'Enter same passphrase again' prompt = first_prompt - for fd in r: + for fd in r_list: if fd == master_out_fd: chunk = os.read(master_out_fd, 10240) out_buffer += chunk diff --git a/lib/ansible/modules/wait_for.py b/lib/ansible/modules/wait_for.py index 439912fe1da..2244f357bc1 100644 --- a/lib/ansible/modules/wait_for.py +++ b/lib/ansible/modules/wait_for.py @@ -603,7 +603,7 @@ def main(): matched = False while datetime.datetime.utcnow() < end: max_timeout = math.ceil(_timedelta_total_seconds(end - datetime.datetime.utcnow())) - (readable, w, e) = select.select([s], [], [], max_timeout) + readable = select.select([s], [], [], max_timeout)[0] if not readable: # No new data. Probably means our timeout # expired diff --git a/lib/ansible/modules/yum.py b/lib/ansible/modules/yum.py index 2f9b8bffaa9..a4955adba04 100644 --- a/lib/ansible/modules/yum.py +++ b/lib/ansible/modules/yum.py @@ -759,12 +759,12 @@ class YumModule(YumDnf): else: raise if not pkgs: - e, m, _ = self.yum_base.pkgSack.matchPackageNames([req_spec]) - pkgs.extend(e) - pkgs.extend(m) - e, m, _ = self.yum_base.rpmdb.matchPackageNames([req_spec]) - pkgs.extend(e) - pkgs.extend(m) + exact_matches, glob_matches = self.yum_base.pkgSack.matchPackageNames([req_spec])[0:2] + pkgs.extend(exact_matches) + pkgs.extend(glob_matches) + exact_matches, glob_matches = self.yum_base.rpmdb.matchPackageNames([req_spec])[0:2] + pkgs.extend(exact_matches) + pkgs.extend(glob_matches) except Exception as e: self.module.fail_json(msg="Failure talking to yum: %s" % to_native(e))