1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

CI: add type checking (#10997)

* Set up type checking with mypy.

* Make mypy pass.

* Use list() instead of sorted().
This commit is contained in:
Felix Fontein 2025-10-29 18:13:38 +01:00 committed by GitHub
parent 831787619a
commit 6088b0cff5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
73 changed files with 442 additions and 175 deletions

View file

@ -11,10 +11,11 @@ import traceback
from operator import itemgetter
PARAMIKO_IMPORT_ERROR: str | None
try:
from paramiko.config import SSHConfig
except ImportError:
SSHConfig = object
SSHConfig = object # type: ignore
HAS_PARAMIKO = False
PARAMIKO_IMPORT_ERROR = traceback.format_exc()
else:

View file

@ -4,10 +4,17 @@
from __future__ import annotations
import typing as t
from functools import wraps
from ansible.module_utils.common.collections import is_sequence
if t.TYPE_CHECKING:
from collections.abc import Callable
ArgFormatType = Callable[[t.Any], list[str]]
def _ensure_list(value):
return list(value) if is_sequence(value) else [value]

View file

@ -9,6 +9,7 @@ from __future__ import annotations
import copy
import json
import re
import typing as t
from urllib import error as urllib_error
from urllib.parse import urlencode
@ -118,12 +119,12 @@ class _ConsulModule:
As such backwards incompatible changes can occur even in bugfix releases.
"""
api_endpoint = None # type: str
unique_identifiers = None # type: list
result_key = None # type: str
create_only_fields = set()
operational_attributes = set()
params = {}
api_endpoint: str | None = None
unique_identifiers: list | None = None
result_key: str | None = None
create_only_fields: set[str] = set()
operational_attributes: set[str] = set()
params: dict[str, t.Any] = {}
def __init__(self, module):
self._module = module

View file

@ -4,12 +4,16 @@
from __future__ import annotations
import typing as t
from ansible.module_utils.common.dict_transformations import dict_merge
from ansible_collections.community.general.plugins.module_utils import cmd_runner_fmt
from ansible_collections.community.general.plugins.module_utils.python_runner import PythonRunner
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
if t.TYPE_CHECKING:
from .cmd_runner_fmt import ArgFormatType
django_std_args = dict(
# environmental options
@ -32,7 +36,7 @@ _pks = dict(
primary_keys=dict(type="list", elements="str"),
)
_django_std_arg_fmts = dict(
_django_std_arg_fmts: dict[str, ArgFormatType] = dict(
all=cmd_runner_fmt.as_bool("--all"),
app=cmd_runner_fmt.as_opt_val("--app"),
apps=cmd_runner_fmt.as_list(),
@ -95,11 +99,11 @@ class _DjangoRunner(PythonRunner):
class DjangoModuleHelper(ModuleHelper):
module = {}
django_admin_cmd = None
arg_formats = {}
django_admin_arg_order = ()
_django_args = []
_check_mode_arg = ""
django_admin_cmd: str | None = None
arg_formats: dict[str, ArgFormatType] = {}
django_admin_arg_order: tuple[str, ...] | str = ()
_django_args: list[str] = []
_check_mode_arg: str = ""
def __init__(self):
self.module["argument_spec"], self.arg_formats = self._build_args(self.module.get("argument_spec", {}),

View file

@ -6,6 +6,8 @@
from __future__ import annotations
import typing as t
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
@ -15,7 +17,7 @@ from urllib.parse import urljoin
import traceback
def _determine_list_all_kwargs(version):
def _determine_list_all_kwargs(version) -> dict[str, t.Any]:
gitlab_version = LooseVersion(version)
if gitlab_version >= LooseVersion('4.0.0'):
# 4.0.0 removed 'as_list'
@ -27,14 +29,14 @@ def _determine_list_all_kwargs(version):
return {'as_list': False, 'all': True, 'per_page': 100}
GITLAB_IMP_ERR = None
GITLAB_IMP_ERR: str | None = None
try:
import gitlab
import requests
HAS_GITLAB_PACKAGE = True
list_all_kwargs = _determine_list_all_kwargs(gitlab.__version__)
except Exception:
gitlab = None
gitlab = None # type: ignore
GITLAB_IMP_ERR = traceback.format_exc()
HAS_GITLAB_PACKAGE = False
list_all_kwargs = {}

View file

@ -20,7 +20,7 @@ from urllib.parse import urlparse
try:
from hashlib import sha1
except ImportError:
import sha as sha1
import sha as sha1 # type: ignore[no-redef]
HASHED_KEY_MAGIC = "|1|"

View file

@ -5,15 +5,17 @@
from __future__ import annotations
import typing as t
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.mh.exceptions import ModuleHelperException as _MHE
from ansible_collections.community.general.plugins.module_utils.mh.deco import module_fails_on_exception
class ModuleHelperBase(object):
module = None
module: dict[str, t.Any] | None = None # TODO: better spec using t.TypedDict
ModuleHelperException = _MHE
_delegated_to_module = (
_delegated_to_module: tuple[str, ...] = (
'check_mode', 'get_bin_path', 'warn', 'deprecate', 'debug',
)

View file

@ -5,13 +5,15 @@
from __future__ import annotations
import typing as t
from ansible.module_utils.common.text.converters import to_native
class ModuleHelperException(Exception):
def __init__(self, msg, update_output=None, *args, **kwargs):
self.msg = to_native(msg or f"Module failed with exception: {self}")
def __init__(self, msg: str, update_output: dict[str, t.Any] | None = None, *args, **kwargs):
self.msg: str = to_native(msg or f"Module failed with exception: {self}")
if update_output is None:
update_output = {}
self.update_output = update_output
self.update_output: dict[str, t.Any] = update_output
super(ModuleHelperException, self).__init__(*args)

View file

@ -7,8 +7,8 @@ from __future__ import annotations
class StateMixin(object):
state_param = 'state'
default_state = None
state_param: str = 'state'
default_state: str | None = None
def _state(self):
state = self.module.params.get(self.state_param)

View file

@ -5,6 +5,7 @@
from __future__ import annotations
import typing as t
from ansible.module_utils.common.dict_transformations import dict_merge
@ -13,13 +14,16 @@ from ansible_collections.community.general.plugins.module_utils.mh.base import M
from ansible_collections.community.general.plugins.module_utils.mh.mixins.state import StateMixin
from ansible_collections.community.general.plugins.module_utils.mh.mixins.deprecate_attrs import DeprecateAttrsMixin
if t.TYPE_CHECKING:
from collections.abc import Sequence
class ModuleHelper(DeprecateAttrsMixin, ModuleHelperBase):
facts_name = None
output_params = ()
diff_params = ()
change_params = ()
facts_params = ()
facts_name: str | None = None
output_params: Sequence[str] = ()
diff_params: Sequence[str] = ()
change_params: Sequence[str] = ()
facts_params: Sequence[str] = ()
def __init__(self, module=None):
super(ModuleHelper, self).__init__(module)

View file

@ -19,12 +19,11 @@ from ansible_collections.community.general.plugins.module_utils.datetime import
now,
)
SCALEWAY_SECRET_IMP_ERR = None
SCALEWAY_SECRET_IMP_ERR: str | None = None
try:
from passlib.hash import argon2
HAS_SCALEWAY_SECRET_PACKAGE = True
except Exception:
argon2 = None
SCALEWAY_SECRET_IMP_ERR = traceback.format_exc()
HAS_SCALEWAY_SECRET_PACKAGE = False

View file

@ -53,7 +53,7 @@ __all__ = [
]
_singletons = {}
_singletons: dict[str, object] = {}
def ldap_module():
@ -61,7 +61,7 @@ def ldap_module():
return orig_ldap
def _singleton(name, constructor):
def _singleton(name: str, constructor):
if name in _singletons:
return _singletons[name]
_singletons[name] = constructor()