mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02: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:
@@ -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',
|
||||
)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user