From 7c81a652c0af1831bb7e0afd0bb23c43e68a40bf Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Mon, 7 Mar 2022 09:46:01 -0800 Subject: [PATCH] Type hint fixes. --- lib/ansible/executor/action_write_locks.py | 4 +++- lib/ansible/executor/module_common.py | 1 + lib/ansible/galaxy/collection/__init__.py | 8 ++++---- lib/ansible/galaxy/collection/galaxy_api_proxy.py | 3 +++ lib/ansible/parsing/yaml/loader.py | 5 ++--- lib/ansible/plugins/__init__.py | 12 +++++++++--- lib/ansible/plugins/connection/paramiko_ssh.py | 4 ++-- lib/ansible/plugins/inventory/toml.py | 1 + lib/ansible/plugins/loader.py | 4 ++-- 9 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/ansible/executor/action_write_locks.py b/lib/ansible/executor/action_write_locks.py index dfc7f9c9274..fd827440ad6 100644 --- a/lib/ansible/executor/action_write_locks.py +++ b/lib/ansible/executor/action_write_locks.py @@ -19,6 +19,8 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import multiprocessing.synchronize + from multiprocessing import Lock from ansible.module_utils.facts.system.pkg_mgr import PKG_MGRS @@ -27,7 +29,7 @@ if 'action_write_locks' not in globals(): # Do not initialize this more than once because it seems to bash # the existing one. multiprocessing must be reloading the module # when it forks? - action_write_locks = dict() + action_write_locks = dict() # type: dict[str | None, multiprocessing.synchronize.Lock] # Below is a Lock for use when we weren't expecting a named module. It gets used when an action # plugin invokes a module whose name does not match with the action's name. Slightly less diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index 912d60d3d3c..f906be67a86 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -29,6 +29,7 @@ import shlex import zipfile import re import pkgutil + from ast import AST, Import, ImportFrom from io import BytesIO diff --git a/lib/ansible/galaxy/collection/__init__.py b/lib/ansible/galaxy/collection/__init__.py index 9b508d36fec..114ec322d9d 100644 --- a/lib/ansible/galaxy/collection/__init__.py +++ b/lib/ansible/galaxy/collection/__init__.py @@ -56,10 +56,10 @@ if t.TYPE_CHECKING: # files meta: FileMetaKeysType, ] - ManifestValueType = dict[CollectionInfoKeysType, 'int | str | list[str] | dict[str, str] | None'] - CollectionManifestType = dict[ManifestKeysType, ManifestValueType] - FileManifestEntryType = dict[FileMetaKeysType, 'str | int | None'] - FilesManifestType = dict[t.Literal['files', 'format'], 'list[FileManifestEntryType] | int'] + ManifestValueType = t.Dict[CollectionInfoKeysType, t.Union[int, str, t.List[str], t.Dict[str, str], None]] + CollectionManifestType = t.Dict[ManifestKeysType, ManifestValueType] + FileManifestEntryType = t.Dict[FileMetaKeysType, t.Union[str, int, None]] + FilesManifestType = t.Dict[t.Literal['files', 'format'], t.Union[t.List[FileManifestEntryType], int]] import ansible.constants as C from ansible.errors import AnsibleError diff --git a/lib/ansible/galaxy/collection/galaxy_api_proxy.py b/lib/ansible/galaxy/collection/galaxy_api_proxy.py index 332645f68f3..ae104c4cb39 100644 --- a/lib/ansible/galaxy/collection/galaxy_api_proxy.py +++ b/lib/ansible/galaxy/collection/galaxy_api_proxy.py @@ -108,6 +108,9 @@ class MultiGalaxyAPIProxy: if isinstance(collection_candidate.src, GalaxyAPI) else self._apis ) + + last_err: t.Optional[Exception] + for api in api_lookup_order: try: version_metadata = api.get_collection_version_metadata( diff --git a/lib/ansible/parsing/yaml/loader.py b/lib/ansible/parsing/yaml/loader.py index d85623abf37..e2989a49eda 100644 --- a/lib/ansible/parsing/yaml/loader.py +++ b/lib/ansible/parsing/yaml/loader.py @@ -25,8 +25,7 @@ from ansible.parsing.yaml.constructor import AnsibleConstructor from ansible.module_utils.common.yaml import HAS_LIBYAML, Parser if HAS_LIBYAML: - - class AnsibleLoader(Parser, AnsibleConstructor, Resolver): + class AnsibleLoader(Parser, AnsibleConstructor, Resolver): # type: ignore[misc] # pylint: disable=inconsistent-mro def __init__(self, stream, file_name=None, vault_secrets=None): Parser.__init__(self, stream) # pylint: disable=non-parent-init-called AnsibleConstructor.__init__(self, file_name=file_name, vault_secrets=vault_secrets) @@ -37,7 +36,7 @@ else: from yaml.scanner import Scanner from yaml.parser import Parser - class AnsibleLoader(Reader, Scanner, Parser, Composer, AnsibleConstructor, Resolver): + class AnsibleLoader(Reader, Scanner, Parser, Composer, AnsibleConstructor, Resolver): # type: ignore[misc,no-redef] # pylint: disable=inconsistent-mro def __init__(self, stream, file_name=None, vault_secrets=None): Reader.__init__(self, stream) Scanner.__init__(self) diff --git a/lib/ansible/plugins/__init__.py b/lib/ansible/plugins/__init__.py index b08e322d898..d3f8630f760 100644 --- a/lib/ansible/plugins/__init__.py +++ b/lib/ansible/plugins/__init__.py @@ -23,6 +23,9 @@ __metaclass__ = type from abc import ABC +import types +import typing as t + from ansible import constants as C from ansible.errors import AnsibleError from ansible.module_utils._text import to_native @@ -31,10 +34,13 @@ from ansible.utils.display import Display display = Display() +if t.TYPE_CHECKING: + from .loader import PluginPathContext + # Global so that all instances of a PluginLoader will share the caches -MODULE_CACHE = {} -PATH_CACHE = {} -PLUGIN_PATH_CACHE = {} +MODULE_CACHE = {} # type: dict[str, dict[str, types.ModuleType]] +PATH_CACHE = {} # type: dict[str, list[PluginPathContext] | None] +PLUGIN_PATH_CACHE = {} # type: dict[str, dict[str, dict[str, PluginPathContext]]] def get_plugin_class(obj): diff --git a/lib/ansible/plugins/connection/paramiko_ssh.py b/lib/ansible/plugins/connection/paramiko_ssh.py index 3511babc109..c3e20d4041b 100644 --- a/lib/ansible/plugins/connection/paramiko_ssh.py +++ b/lib/ansible/plugins/connection/paramiko_ssh.py @@ -223,8 +223,8 @@ class MyAddPolicy(object): # keep connection objects on a per host basis to avoid repeated attempts to reconnect -SSH_CONNECTION_CACHE = {} -SFTP_CONNECTION_CACHE = {} +SSH_CONNECTION_CACHE = {} # type: dict[str, paramiko.client.SSHClient] +SFTP_CONNECTION_CACHE = {} # type: dict[str, paramiko.sftp_client.SFTPClient] class Connection(ConnectionBase): diff --git a/lib/ansible/plugins/inventory/toml.py b/lib/ansible/plugins/inventory/toml.py index af75ddb7dfb..b98987ed272 100644 --- a/lib/ansible/plugins/inventory/toml.py +++ b/lib/ansible/plugins/inventory/toml.py @@ -87,6 +87,7 @@ host4 = {} ''' import os +import typing as t from functools import partial diff --git a/lib/ansible/plugins/loader.py b/lib/ansible/plugins/loader.py index 59ebd4b142f..019a5bcf24f 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -35,8 +35,8 @@ try: from packaging.specifiers import SpecifierSet from packaging.version import Version except ImportError: - SpecifierSet = None - Version = None + SpecifierSet = None # type: ignore[misc] + Version = None # type: ignore[misc] import importlib.util