module_utils - Fix type hinting issues.

pull/77188/head
Matt Clay 3 years ago
parent 8e45925415
commit 1286513947

@ -360,7 +360,7 @@ def _return_datastructure_name(obj):
for element in obj: for element in obj:
for subelement in _return_datastructure_name(element): for subelement in _return_datastructure_name(element):
yield subelement 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 # This must come before int because bools are also ints
return return
elif isinstance(obj, tuple(list(integer_types) + [float])): elif isinstance(obj, tuple(list(integer_types) + [float])):

@ -9,8 +9,15 @@ preferring the YAML compiled C extensions to reduce duplicated code.
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import types
from functools import partial as _partial from functools import partial as _partial
try:
import typing as t
except ImportError:
t = None # type: types.ModuleType | None
HAS_LIBYAML = False HAS_LIBYAML = False
try: try:
import yaml as _yaml import yaml as _yaml
@ -31,9 +38,9 @@ else:
Parser = _yaml.cyaml.CParser Parser = _yaml.cyaml.CParser
HAS_LIBYAML = True HAS_LIBYAML = True
except AttributeError: except AttributeError:
SafeLoader = _yaml.SafeLoader SafeLoader = _yaml.SafeLoader # type: t.Type[_yaml.CSafeLoader] | t.Type[_yaml.SafeLoader]
SafeDumper = _yaml.SafeDumper SafeDumper = _yaml.SafeDumper # type: t.Type[_yaml.CSafeDumper] | t.Type[_yaml.SafeDumper]
Parser = _yaml.parser.Parser Parser = _yaml.parser.Parser # type: t.Type[_yaml.cyaml.CParser] | t.Type[_yaml.parser.Parser]
yaml_load = _partial(_yaml.load, Loader=SafeLoader) yaml_load = _partial(_yaml.load, Loader=SafeLoader)
yaml_load_all = _partial(_yaml.load_all, Loader=SafeLoader) yaml_load_all = _partial(_yaml.load_all, Loader=SafeLoader)

@ -13,6 +13,6 @@ except ImportError:
# importlib.import_module returns the tail # importlib.import_module returns the tail
# whereas __import__ returns the head # whereas __import__ returns the head
# compat to work like importlib.import_module # compat to work like importlib.import_module
def import_module(name): def import_module(name): # type: ignore[misc]
__import__(name) __import__(name)
return sys.modules[name] return sys.modules[name]

@ -37,6 +37,7 @@ _BUNDLED_METADATA = {"pypi_name": "selectors2", "version": "1.1.1", "version_con
import os.path import os.path
import sys import sys
import types
try: try:
# Python 3.4+ # Python 3.4+
@ -46,7 +47,7 @@ except ImportError:
# backport package installed in the system # backport package installed in the system
import selectors2 as _system_selectors import selectors2 as _system_selectors
except ImportError: except ImportError:
_system_selectors = None _system_selectors = None # type: types.ModuleType | None
if _system_selectors: if _system_selectors:
selectors = _system_selectors selectors = _system_selectors

@ -28,6 +28,14 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __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.facter import FacterFactCollector
from ansible.module_utils.facts.other.ohai import OhaiFactCollector from ansible.module_utils.facts.other.ohai import OhaiFactCollector
@ -93,7 +101,7 @@ _base = [
PlatformFactCollector, PlatformFactCollector,
DistributionFactCollector, DistributionFactCollector,
LSBFactCollector LSBFactCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
# These restrict what is possible in others # These restrict what is possible in others
_restrictive = [ _restrictive = [
@ -101,7 +109,7 @@ _restrictive = [
ApparmorFactCollector, ApparmorFactCollector,
ChrootFactCollector, ChrootFactCollector,
FipsFactCollector FipsFactCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
# general info, not required but probably useful for other facts # general info, not required but probably useful for other facts
_general = [ _general = [
@ -115,7 +123,7 @@ _general = [
EnvFactCollector, EnvFactCollector,
SshPubKeyFactCollector, SshPubKeyFactCollector,
UserFactCollector UserFactCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
# virtual, this might also limit hardware/networking # virtual, this might also limit hardware/networking
_virtual = [ _virtual = [
@ -127,7 +135,7 @@ _virtual = [
NetBSDVirtualCollector, NetBSDVirtualCollector,
SunOSVirtualCollector, SunOSVirtualCollector,
HPUXVirtualCollector HPUXVirtualCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
_hardware = [ _hardware = [
HardwareCollector, HardwareCollector,
@ -141,7 +149,7 @@ _hardware = [
NetBSDHardwareCollector, NetBSDHardwareCollector,
OpenBSDHardwareCollector, OpenBSDHardwareCollector,
SunOSHardwareCollector SunOSHardwareCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
_network = [ _network = [
DnsFactCollector, DnsFactCollector,
@ -159,14 +167,14 @@ _network = [
NetBSDNetworkCollector, NetBSDNetworkCollector,
OpenBSDNetworkCollector, OpenBSDNetworkCollector,
SunOSNetworkCollector SunOSNetworkCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
# other fact sources # other fact sources
_extra_facts = [ _extra_facts = [
LocalFactCollector, LocalFactCollector,
FacterFactCollector, FacterFactCollector,
OhaiFactCollector OhaiFactCollector
] ] # type: t.List[t.Type[BaseFactCollector]]
# TODO: make config driven # TODO: make config driven
collectors = _base + _restrictive + _general + _virtual + _hardware + _network + _extra_facts collectors = _base + _restrictive + _general + _virtual + _hardware + _network + _extra_facts

@ -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)} 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 @abstractmethod
def is_available(self): def is_available(self):

@ -58,7 +58,7 @@ except ImportError:
from compiler import ast, parse from compiler import ast, parse
from ansible.module_utils.six import binary_type, integer_types, string_types, text_type 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 Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following expression. The string or node provided may only consist of the following

@ -52,6 +52,7 @@ import socket
import sys import sys
import tempfile import tempfile
import traceback import traceback
import types
from contextlib import contextmanager from contextlib import contextmanager
@ -167,7 +168,7 @@ try:
from ssl import match_hostname, CertificateError from ssl import match_hostname, CertificateError
except ImportError: except ImportError:
try: try:
from backports.ssl_match_hostname import match_hostname, CertificateError from backports.ssl_match_hostname import match_hostname, CertificateError # type: ignore[misc]
except ImportError: except ImportError:
HAS_MATCH_HOSTNAME = False HAS_MATCH_HOSTNAME = False
@ -268,7 +269,7 @@ try:
except ImportError: except ImportError:
GSSAPI_IMP_ERR = traceback.format_exc() GSSAPI_IMP_ERR = traceback.format_exc()
HTTPGSSAPIAuthHandler = None HTTPGSSAPIAuthHandler = None # type: types.ModuleType | None
if not HAS_MATCH_HOSTNAME: if not HAS_MATCH_HOSTNAME:
# The following block of code is under the terms and conditions of the # The following block of code is under the terms and conditions of the
@ -279,7 +280,7 @@ if not HAS_MATCH_HOSTNAME:
try: try:
# Divergence: Python-3.7+'s _ssl has this exception type but older Pythons do not # Divergence: Python-3.7+'s _ssl has this exception type but older Pythons do not
from _ssl import SSLCertVerificationError from _ssl import SSLCertVerificationError
CertificateError = SSLCertVerificationError CertificateError = SSLCertVerificationError # type: ignore[misc]
except ImportError: except ImportError:
class CertificateError(ValueError): class CertificateError(ValueError):
pass pass
@ -390,7 +391,7 @@ if not HAS_MATCH_HOSTNAME:
ip = _inet_paton(ipname.rstrip()) ip = _inet_paton(ipname.rstrip())
return ip == host_ip 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 """Verify that *cert* (in decoded format as returned by
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
rules are followed. rules are followed.

@ -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 Abstract class that handles the population of instance variables that should
be identical between both YUM and DNF modules because of the feature parity be identical between both YUM and DNF modules because of the feature parity

Loading…
Cancel
Save