diff --git a/lib/ansible/module_utils/common/parameters.py b/lib/ansible/module_utils/common/parameters.py index a9bbcb28d23..93b80431c36 100644 --- a/lib/ansible/module_utils/common/parameters.py +++ b/lib/ansible/module_utils/common/parameters.py @@ -360,7 +360,7 @@ def _return_datastructure_name(obj): for element in obj: for subelement in _return_datastructure_name(element): yield subelement - elif isinstance(obj, (bool, NoneType)): + elif obj is None or isinstance(obj, bool): # This must come before int because bools are also ints return elif isinstance(obj, tuple(list(integer_types) + [float])): diff --git a/lib/ansible/module_utils/common/yaml.py b/lib/ansible/module_utils/common/yaml.py index 4d64dbb6c45..1e4d9ce2b3b 100644 --- a/lib/ansible/module_utils/common/yaml.py +++ b/lib/ansible/module_utils/common/yaml.py @@ -9,8 +9,15 @@ preferring the YAML compiled C extensions to reduce duplicated code. from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import types + from functools import partial as _partial +try: + import typing as t +except ImportError: + t = None # type: types.ModuleType | None + HAS_LIBYAML = False try: import yaml as _yaml @@ -31,9 +38,9 @@ else: Parser = _yaml.cyaml.CParser HAS_LIBYAML = True except AttributeError: - SafeLoader = _yaml.SafeLoader - SafeDumper = _yaml.SafeDumper - Parser = _yaml.parser.Parser + SafeLoader = _yaml.SafeLoader # type: t.Type[_yaml.CSafeLoader] | t.Type[_yaml.SafeLoader] + SafeDumper = _yaml.SafeDumper # type: t.Type[_yaml.CSafeDumper] | t.Type[_yaml.SafeDumper] + Parser = _yaml.parser.Parser # type: t.Type[_yaml.cyaml.CParser] | t.Type[_yaml.parser.Parser] yaml_load = _partial(_yaml.load, Loader=SafeLoader) yaml_load_all = _partial(_yaml.load_all, Loader=SafeLoader) diff --git a/lib/ansible/module_utils/compat/importlib.py b/lib/ansible/module_utils/compat/importlib.py index eee0ddf7bcf..0b7fb2c78e2 100644 --- a/lib/ansible/module_utils/compat/importlib.py +++ b/lib/ansible/module_utils/compat/importlib.py @@ -13,6 +13,6 @@ except ImportError: # importlib.import_module returns the tail # whereas __import__ returns the head # compat to work like importlib.import_module - def import_module(name): + def import_module(name): # type: ignore[misc] __import__(name) return sys.modules[name] diff --git a/lib/ansible/module_utils/compat/selectors.py b/lib/ansible/module_utils/compat/selectors.py index 53996d7e017..d664aeab287 100644 --- a/lib/ansible/module_utils/compat/selectors.py +++ b/lib/ansible/module_utils/compat/selectors.py @@ -37,6 +37,7 @@ _BUNDLED_METADATA = {"pypi_name": "selectors2", "version": "1.1.1", "version_con import os.path import sys +import types try: # Python 3.4+ @@ -46,7 +47,7 @@ except ImportError: # backport package installed in the system import selectors2 as _system_selectors except ImportError: - _system_selectors = None + _system_selectors = None # type: types.ModuleType | None if _system_selectors: selectors = _system_selectors diff --git a/lib/ansible/module_utils/facts/default_collectors.py b/lib/ansible/module_utils/facts/default_collectors.py index 3ac35704942..98b27e36369 100644 --- a/lib/ansible/module_utils/facts/default_collectors.py +++ b/lib/ansible/module_utils/facts/default_collectors.py @@ -28,6 +28,14 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import types + +try: + import typing as t +except ImportError: + t = None # type: types.ModuleType | None + +from ansible.module_utils.facts.collector import BaseFactCollector from ansible.module_utils.facts.other.facter import FacterFactCollector from ansible.module_utils.facts.other.ohai import OhaiFactCollector @@ -93,7 +101,7 @@ _base = [ PlatformFactCollector, DistributionFactCollector, LSBFactCollector -] +] # type: t.List[t.Type[BaseFactCollector]] # These restrict what is possible in others _restrictive = [ @@ -101,7 +109,7 @@ _restrictive = [ ApparmorFactCollector, ChrootFactCollector, FipsFactCollector -] +] # type: t.List[t.Type[BaseFactCollector]] # general info, not required but probably useful for other facts _general = [ @@ -115,7 +123,7 @@ _general = [ EnvFactCollector, SshPubKeyFactCollector, UserFactCollector -] +] # type: t.List[t.Type[BaseFactCollector]] # virtual, this might also limit hardware/networking _virtual = [ @@ -127,7 +135,7 @@ _virtual = [ NetBSDVirtualCollector, SunOSVirtualCollector, HPUXVirtualCollector -] +] # type: t.List[t.Type[BaseFactCollector]] _hardware = [ HardwareCollector, @@ -141,7 +149,7 @@ _hardware = [ NetBSDHardwareCollector, OpenBSDHardwareCollector, SunOSHardwareCollector -] +] # type: t.List[t.Type[BaseFactCollector]] _network = [ DnsFactCollector, @@ -159,14 +167,14 @@ _network = [ NetBSDNetworkCollector, OpenBSDNetworkCollector, SunOSNetworkCollector -] +] # type: t.List[t.Type[BaseFactCollector]] # other fact sources _extra_facts = [ LocalFactCollector, FacterFactCollector, OhaiFactCollector -] +] # type: t.List[t.Type[BaseFactCollector]] # TODO: make config driven collectors = _base + _restrictive + _general + _virtual + _hardware + _network + _extra_facts diff --git a/lib/ansible/module_utils/facts/packages.py b/lib/ansible/module_utils/facts/packages.py index ff1f5be9475..53f74a16620 100644 --- a/lib/ansible/module_utils/facts/packages.py +++ b/lib/ansible/module_utils/facts/packages.py @@ -16,7 +16,7 @@ def get_all_pkg_managers(): return {obj.__name__.lower(): obj for obj in get_all_subclasses(PkgMgr) if obj not in (CLIMgr, LibMgr)} -class PkgMgr(with_metaclass(ABCMeta, object)): +class PkgMgr(with_metaclass(ABCMeta, object)): # type: ignore[misc] @abstractmethod def is_available(self): diff --git a/lib/ansible/module_utils/pycompat24.py b/lib/ansible/module_utils/pycompat24.py index 0c050dffad1..c398427ca43 100644 --- a/lib/ansible/module_utils/pycompat24.py +++ b/lib/ansible/module_utils/pycompat24.py @@ -58,7 +58,7 @@ except ImportError: from compiler import ast, parse from ansible.module_utils.six import binary_type, integer_types, string_types, text_type - def literal_eval(node_or_string): + def literal_eval(node_or_string): # type: ignore[misc] """ Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index d16a6a935d1..4eab8f1d339 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -52,6 +52,7 @@ import socket import sys import tempfile import traceback +import types from contextlib import contextmanager @@ -167,7 +168,7 @@ try: from ssl import match_hostname, CertificateError except ImportError: try: - from backports.ssl_match_hostname import match_hostname, CertificateError + from backports.ssl_match_hostname import match_hostname, CertificateError # type: ignore[misc] except ImportError: HAS_MATCH_HOSTNAME = False @@ -268,7 +269,7 @@ try: except ImportError: GSSAPI_IMP_ERR = traceback.format_exc() - HTTPGSSAPIAuthHandler = None + HTTPGSSAPIAuthHandler = None # type: types.ModuleType | None if not HAS_MATCH_HOSTNAME: # The following block of code is under the terms and conditions of the @@ -279,7 +280,7 @@ if not HAS_MATCH_HOSTNAME: try: # Divergence: Python-3.7+'s _ssl has this exception type but older Pythons do not from _ssl import SSLCertVerificationError - CertificateError = SSLCertVerificationError + CertificateError = SSLCertVerificationError # type: ignore[misc] except ImportError: class CertificateError(ValueError): pass @@ -390,7 +391,7 @@ if not HAS_MATCH_HOSTNAME: ip = _inet_paton(ipname.rstrip()) return ip == host_ip - def match_hostname(cert, hostname): + def match_hostname(cert, hostname): # type: ignore[misc] """Verify that *cert* (in decoded format as returned by SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125 rules are followed. diff --git a/lib/ansible/module_utils/yumdnf.py b/lib/ansible/module_utils/yumdnf.py index 018f0d25ec4..e265a2d34f5 100644 --- a/lib/ansible/module_utils/yumdnf.py +++ b/lib/ansible/module_utils/yumdnf.py @@ -59,7 +59,7 @@ yumdnf_argument_spec = dict( ) -class YumDnf(with_metaclass(ABCMeta, object)): +class YumDnf(with_metaclass(ABCMeta, object)): # type: ignore[misc] """ Abstract class that handles the population of instance variables that should be identical between both YUM and DNF modules because of the feature parity