mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-09 21:47:17 +00:00
[PR #11283/ef632145 backport][stable-12] Add more module_utils typing (#11289)
Add more module_utils typing (#11283)
Add more module_utils typing.
(cherry picked from commit ef632145e9)
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
8590184232
commit
21aa086ca6
12 changed files with 340 additions and 274 deletions
|
|
@ -40,7 +40,7 @@ class ModuleHelperBase:
|
|||
def verbosity(self):
|
||||
return self.module._verbosity
|
||||
|
||||
def do_raise(self, *args, **kwargs):
|
||||
def do_raise(self, *args, **kwargs) -> t.NoReturn:
|
||||
raise _MHE(*args, **kwargs)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
|
|
@ -61,14 +61,14 @@ class ModuleHelperBase:
|
|||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def changed(self):
|
||||
def changed(self) -> bool:
|
||||
try:
|
||||
return self.__changed__()
|
||||
except NotImplementedError:
|
||||
return self._changed
|
||||
|
||||
@changed.setter
|
||||
def changed(self, value):
|
||||
def changed(self, value: bool) -> None:
|
||||
self._changed = value
|
||||
|
||||
def has_changed(self):
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import typing as t
|
|||
|
||||
|
||||
class ModuleHelperException(Exception):
|
||||
def __init__(self, msg: str, update_output: dict[str, t.Any] | None = None, *args, **kwargs):
|
||||
def __init__(self, msg: str, update_output: dict[str, t.Any] | None = None, *args, **kwargs) -> None:
|
||||
self.msg: str = msg or f"Module failed with exception: {self}"
|
||||
if update_output is None:
|
||||
update_output = {}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,15 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
class DeprecateAttrsMixin:
|
||||
def _deprecate_setup(self, attr, target, module):
|
||||
def _deprecate_setup(
|
||||
self, attr: str, target: object | None, module: AnsibleModule | None
|
||||
) -> tuple[object, AnsibleModule, dict[str, t.Any], dict[str, t.Any]]:
|
||||
if target is None:
|
||||
target = self
|
||||
if not hasattr(target, attr):
|
||||
|
|
@ -37,8 +40,16 @@ class DeprecateAttrsMixin:
|
|||
return target, module, value_dict, trigger_dict
|
||||
|
||||
def _deprecate_attr(
|
||||
self, attr, msg, version=None, date=None, collection_name=None, target=None, value=None, module=None
|
||||
):
|
||||
self,
|
||||
attr: str,
|
||||
msg: str,
|
||||
version: str | None = None,
|
||||
date: str | None = None,
|
||||
collection_name: str | None = None,
|
||||
target: object | None = None,
|
||||
value=None,
|
||||
module: AnsibleModule | None = None,
|
||||
) -> None:
|
||||
target, module, value_dict, trigger_dict = self._deprecate_setup(attr, target, module)
|
||||
|
||||
value_dict[attr] = getattr(target, attr, value)
|
||||
|
|
|
|||
|
|
@ -5,16 +5,18 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import typing as t
|
||||
|
||||
|
||||
class StateMixin:
|
||||
state_param: str = "state"
|
||||
default_state: str | None = None
|
||||
|
||||
def _state(self):
|
||||
state = self.module.params.get(self.state_param)
|
||||
def _state(self) -> str:
|
||||
state: str = self.module.params.get(self.state_param) # type: ignore[attr-defined]
|
||||
return self.default_state if state is None else state
|
||||
|
||||
def _method(self, state):
|
||||
def _method(self, state: str) -> str:
|
||||
return f"{self.state_param}_{state}"
|
||||
|
||||
def __run__(self):
|
||||
|
|
@ -34,5 +36,5 @@ class StateMixin:
|
|||
func = getattr(self, method)
|
||||
return func()
|
||||
|
||||
def __state_fallback__(self):
|
||||
def __state_fallback__(self) -> t.NoReturn:
|
||||
raise ValueError(f"Cannot find method: {self._method(self._state())}")
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ from ansible_collections.community.general.plugins.module_utils.mh.mixins.deprec
|
|||
|
||||
if t.TYPE_CHECKING:
|
||||
from collections.abc import Sequence
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
class ModuleHelper(DeprecateAttrsMixin, ModuleHelperBase):
|
||||
|
|
@ -25,11 +26,11 @@ class ModuleHelper(DeprecateAttrsMixin, ModuleHelperBase):
|
|||
change_params: Sequence[str] = ()
|
||||
facts_params: Sequence[str] = ()
|
||||
|
||||
def __init__(self, module=None):
|
||||
def __init__(self, module: AnsibleModule | dict[str, t.Any] | None = None) -> None:
|
||||
super().__init__(module)
|
||||
|
||||
self.vars = VarDict()
|
||||
for name, value in self.module.params.items():
|
||||
for name, value in self.module.params.items(): # type: ignore[union-attr]
|
||||
self.vars.set(
|
||||
name,
|
||||
value,
|
||||
|
|
@ -39,26 +40,26 @@ class ModuleHelper(DeprecateAttrsMixin, ModuleHelperBase):
|
|||
fact=name in self.facts_params,
|
||||
)
|
||||
|
||||
def update_vars(self, meta=None, **kwargs):
|
||||
def update_vars(self, meta: dict[str, t.Any] | None = None, **kwargs: t.Any) -> None:
|
||||
if meta is None:
|
||||
meta = {}
|
||||
for k, v in kwargs.items():
|
||||
self.vars.set(k, v, **meta)
|
||||
|
||||
def update_output(self, **kwargs):
|
||||
def update_output(self, **kwargs: t.Any) -> None:
|
||||
self.update_vars(meta={"output": True}, **kwargs)
|
||||
|
||||
def update_facts(self, **kwargs):
|
||||
def update_facts(self, **kwargs: t.Any) -> None:
|
||||
self.update_vars(meta={"fact": True}, **kwargs)
|
||||
|
||||
def _vars_changed(self):
|
||||
def _vars_changed(self) -> bool:
|
||||
return self.vars.has_changed
|
||||
|
||||
def has_changed(self):
|
||||
def has_changed(self) -> bool:
|
||||
return self.changed or self._vars_changed()
|
||||
|
||||
@property
|
||||
def output(self):
|
||||
def output(self) -> dict[str, t.Any]:
|
||||
result = dict(self.vars.output())
|
||||
if self.facts_name:
|
||||
facts = self.vars.facts()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue