diff --git a/lib/ansible/module_utils/common/yaml.py b/lib/ansible/module_utils/common/yaml.py index 1e4d9ce2b3b..cba90d5457b 100644 --- a/lib/ansible/module_utils/common/yaml.py +++ b/lib/ansible/module_utils/common/yaml.py @@ -16,7 +16,7 @@ from functools import partial as _partial try: import typing as t except ImportError: - t = None # type: types.ModuleType | None + t = None # type: types.ModuleType | None # type: ignore[no-redef] HAS_LIBYAML = False try: @@ -38,9 +38,9 @@ else: Parser = _yaml.cyaml.CParser HAS_LIBYAML = True except AttributeError: - 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] + SafeLoader = _yaml.SafeLoader # type: t.Type[_yaml.CSafeLoader] | t.Type[_yaml.SafeLoader] # type: ignore[no-redef] + SafeDumper = _yaml.SafeDumper # type: t.Type[_yaml.CSafeDumper] | t.Type[_yaml.SafeDumper] # type: ignore[no-redef] + Parser = _yaml.parser.Parser # type: t.Type[_yaml.cyaml.CParser] | t.Type[_yaml.parser.Parser] # type: ignore[no-redef] yaml_load = _partial(_yaml.load, Loader=SafeLoader) yaml_load_all = _partial(_yaml.load_all, Loader=SafeLoader) diff --git a/lib/ansible/module_utils/compat/paramiko.py b/lib/ansible/module_utils/compat/paramiko.py index 8e09b1b893a..3a508cae757 100644 --- a/lib/ansible/module_utils/compat/paramiko.py +++ b/lib/ansible/module_utils/compat/paramiko.py @@ -5,13 +5,15 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +import types + PARAMIKO_IMPORT_ERR = None -paramiko = None try: import paramiko # paramiko and gssapi are incompatible and raise AttributeError not ImportError # When running in FIPS mode, cryptography raises InternalError # https://bugzilla.redhat.com/show_bug.cgi?id=1778939 except Exception as err: + paramiko = None # type: types.ModuleType | None # type: ignore[no-redef] PARAMIKO_IMPORT_ERR = err diff --git a/lib/ansible/module_utils/compat/selectors.py b/lib/ansible/module_utils/compat/selectors.py index d664aeab287..93ffc626297 100644 --- a/lib/ansible/module_utils/compat/selectors.py +++ b/lib/ansible/module_utils/compat/selectors.py @@ -45,13 +45,13 @@ try: except ImportError: try: # backport package installed in the system - import selectors2 as _system_selectors + import selectors2 as _system_selectors # type: ignore[no-redef] except ImportError: - _system_selectors = None # type: types.ModuleType | None + _system_selectors = None # type: types.ModuleType | None # type: ignore[no-redef] if _system_selectors: selectors = _system_selectors else: # Our bundled copy - from ansible.module_utils.compat import _selectors2 as selectors + from ansible.module_utils.compat import _selectors2 as selectors # type: ignore[no-redef] sys.modules['ansible.module_utils.compat.selectors'] = selectors diff --git a/lib/ansible/module_utils/facts/default_collectors.py b/lib/ansible/module_utils/facts/default_collectors.py index 98b27e36369..3b88775501e 100644 --- a/lib/ansible/module_utils/facts/default_collectors.py +++ b/lib/ansible/module_utils/facts/default_collectors.py @@ -33,7 +33,7 @@ import types try: import typing as t except ImportError: - t = None # type: types.ModuleType | None + t = None # type: types.ModuleType | None # type: ignore[no-redef] from ansible.module_utils.facts.collector import BaseFactCollector diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 4eab8f1d339..d224f9e7f3d 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -66,10 +66,9 @@ try: import httplib except ImportError: # Python 3 - import http.client as httplib + import http.client as httplib # type: ignore[no-redef] import ansible.module_utils.six.moves.http_cookiejar as cookiejar -import ansible.module_utils.six.moves.urllib.request as urllib_request import ansible.module_utils.six.moves.urllib.error as urllib_error from ansible.module_utils.common.collections import Mapping @@ -84,8 +83,8 @@ try: from urllib.request import AbstractHTTPHandler, BaseHandler except ImportError: # python2 - import urllib2 as urllib_request - from urllib2 import AbstractHTTPHandler, BaseHandler + import urllib2 as urllib_request # type: ignore[no-redef] + from urllib2 import AbstractHTTPHandler, BaseHandler # type: ignore[no-redef] urllib_request.HTTPRedirectHandler.http_error_308 = urllib_request.HTTPRedirectHandler.http_error_307 @@ -269,7 +268,7 @@ try: except ImportError: GSSAPI_IMP_ERR = traceback.format_exc() - HTTPGSSAPIAuthHandler = None # type: types.ModuleType | None + HTTPGSSAPIAuthHandler = None # type: types.ModuleType | None # type: ignore[no-redef] if not HAS_MATCH_HOSTNAME: # The following block of code is under the terms and conditions of the @@ -282,7 +281,7 @@ if not HAS_MATCH_HOSTNAME: from _ssl import SSLCertVerificationError CertificateError = SSLCertVerificationError # type: ignore[misc] except ImportError: - class CertificateError(ValueError): + class CertificateError(ValueError): # type: ignore[no-redef] pass def _dnsname_match(dn, hostname): @@ -520,7 +519,7 @@ CustomHTTPSHandler = None HTTPSClientAuthHandler = None UnixHTTPSConnection = None if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler'): - class CustomHTTPSConnection(httplib.HTTPSConnection): + class CustomHTTPSConnection(httplib.HTTPSConnection): # type: ignore[no-redef] def __init__(self, *args, **kwargs): httplib.HTTPSConnection.__init__(self, *args, **kwargs) self.context = None @@ -555,7 +554,7 @@ if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler else: self.sock = ssl.wrap_socket(sock, keyfile=self.key_file, certfile=self.cert_file, ssl_version=PROTOCOL) - class CustomHTTPSHandler(urllib_request.HTTPSHandler): + class CustomHTTPSHandler(urllib_request.HTTPSHandler): # type: ignore[no-redef] def https_open(self, req): kwargs = {} @@ -571,7 +570,7 @@ if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler https_request = AbstractHTTPHandler.do_request_ - class HTTPSClientAuthHandler(urllib_request.HTTPSHandler): + class HTTPSClientAuthHandler(urllib_request.HTTPSHandler): # type: ignore[no-redef] '''Handles client authentication via cert/key This is a fairly lightweight extension on HTTPSHandler, and can be used @@ -611,7 +610,7 @@ if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler yield httplib.HTTPConnection.connect = _connect - class UnixHTTPSConnection(httplib.HTTPSConnection): + class UnixHTTPSConnection(httplib.HTTPSConnection): # type: ignore[no-redef] def __init__(self, unix_socket): self._unix_socket = unix_socket diff --git a/lib/ansible/modules/hostname.py b/lib/ansible/modules/hostname.py index 76636351263..edffd903e4a 100644 --- a/lib/ansible/modules/hostname.py +++ b/lib/ansible/modules/hostname.py @@ -75,7 +75,7 @@ import types try: import typing as t except ImportError: - t = None # type: types.ModuleType | None + t = None # type: types.ModuleType | None # type: ignore[no-redef] from ansible.module_utils.basic import ( AnsibleModule, diff --git a/lib/ansible/modules/pip.py b/lib/ansible/modules/pip.py index 8814ae7869b..21327bc7b54 100644 --- a/lib/ansible/modules/pip.py +++ b/lib/ansible/modules/pip.py @@ -458,7 +458,7 @@ def _have_pip_module(): # type: () -> bool try: import importlib except ImportError: - importlib = None # type: types.ModuleType | None + importlib = None # type: types.ModuleType | None # type: ignore[no-redef] if importlib: # noinspection PyBroadException diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 58af783865e..2be802edd10 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -139,6 +139,7 @@ lib/ansible/module_utils/six/__init__.py no-dict-iterkeys lib/ansible/module_utils/six/__init__.py no-dict-itervalues lib/ansible/module_utils/six/__init__.py pylint:self-assigning-variable lib/ansible/module_utils/six/__init__.py replace-urlopen +lib/ansible/module_utils/urls.py pylint:arguments-renamed lib/ansible/module_utils/urls.py pylint:disallowed-name lib/ansible/module_utils/urls.py replace-urlopen lib/ansible/parsing/vault/__init__.py pylint:disallowed-name