Fix type hints and variable usage in modules. (#77184)

* Remove unused variables.
* Fix conflicting variable name.
* Add type hints.
pull/77185/head
Matt Clay 3 years ago committed by GitHub
parent 6de04e8be6
commit 35ef2af0ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -70,11 +70,12 @@ import os
import platform import platform
import socket import socket
import traceback import traceback
import types
try: try:
import typing as t import typing as t
except ImportError: except ImportError:
t = None t = None # type: types.ModuleType | None
from ansible.module_utils.basic import ( from ansible.module_utils.basic import (
AnsibleModule, AnsibleModule,
@ -657,7 +658,7 @@ class SLESHostname(Hostname):
distribution_version = get_distribution_version() distribution_version = get_distribution_version()
# cast to float may raise ValueError on non SLES, we use float for a little more safety over int # 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: if distribution_version and 10 <= float(distribution_version) <= 12:
strategy_class = SLESStrategy strategy_class = SLESStrategy # type: t.Type[BaseStrategy]
else: else:
raise ValueError() raise ValueError()
except ValueError: except ValueError:

@ -272,6 +272,8 @@ import tempfile
import operator import operator
import shlex import shlex
import traceback import traceback
import types
from ansible.module_utils.compat.version import LooseVersion from ansible.module_utils.compat.version import LooseVersion
SETUPTOOLS_IMP_ERR = None SETUPTOOLS_IMP_ERR = None
@ -456,7 +458,7 @@ def _have_pip_module(): # type: () -> bool
try: try:
import importlib import importlib
except ImportError: except ImportError:
importlib = None importlib = None # type: types.ModuleType | None
if importlib: if importlib:
# noinspection PyBroadException # noinspection PyBroadException

@ -499,7 +499,7 @@ class User(object):
platform = 'Generic' platform = 'Generic'
distribution = None # type: str | None distribution = None # type: str | None
PASSWORDFILE = '/etc/passwd' PASSWORDFILE = '/etc/passwd'
SHADOWFILE = '/etc/shadow' SHADOWFILE = '/etc/shadow' # type: str | None
SHADOWFILE_EXPIRE_INDEX = 7 SHADOWFILE_EXPIRE_INDEX = 7
LOGIN_DEFS = '/etc/login.defs' LOGIN_DEFS = '/etc/login.defs'
DATE_FORMAT = '%Y-%m-%d' DATE_FORMAT = '%Y-%m-%d'
@ -1167,11 +1167,11 @@ class User(object):
out_buffer = b'' out_buffer = b''
err_buffer = b'' err_buffer = b''
while p.poll() is None: 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):' first_prompt = b'Enter passphrase (empty for no passphrase):'
second_prompt = b'Enter same passphrase again' second_prompt = b'Enter same passphrase again'
prompt = first_prompt prompt = first_prompt
for fd in r: for fd in r_list:
if fd == master_out_fd: if fd == master_out_fd:
chunk = os.read(master_out_fd, 10240) chunk = os.read(master_out_fd, 10240)
out_buffer += chunk out_buffer += chunk

@ -603,7 +603,7 @@ def main():
matched = False matched = False
while datetime.datetime.utcnow() < end: while datetime.datetime.utcnow() < end:
max_timeout = math.ceil(_timedelta_total_seconds(end - datetime.datetime.utcnow())) 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: if not readable:
# No new data. Probably means our timeout # No new data. Probably means our timeout
# expired # expired

@ -759,12 +759,12 @@ class YumModule(YumDnf):
else: else:
raise raise
if not pkgs: if not pkgs:
e, m, _ = self.yum_base.pkgSack.matchPackageNames([req_spec]) exact_matches, glob_matches = self.yum_base.pkgSack.matchPackageNames([req_spec])[0:2]
pkgs.extend(e) pkgs.extend(exact_matches)
pkgs.extend(m) pkgs.extend(glob_matches)
e, m, _ = self.yum_base.rpmdb.matchPackageNames([req_spec]) exact_matches, glob_matches = self.yum_base.rpmdb.matchPackageNames([req_spec])[0:2]
pkgs.extend(e) pkgs.extend(exact_matches)
pkgs.extend(m) pkgs.extend(glob_matches)
except Exception as e: except Exception as e:
self.module.fail_json(msg="Failure talking to yum: %s" % to_native(e)) self.module.fail_json(msg="Failure talking to yum: %s" % to_native(e))

Loading…
Cancel
Save