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
73 changed files with 442 additions and 175 deletions

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)