mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-14 07:55:05 +00:00
[PR #11222/c7f6a28d backport][stable-12] Add basic typing for module_utils (#11243)
Add basic typing for module_utils (#11222) * Add basic typing for module_utils. * Apply some suggestions. * Make pass again. * Add more types as suggested. * Normalize extra imports. * Add more type hints. * Improve typing. * Add changelog fragment. * Reduce changelog. * Apply suggestions from code review. * Fix typo. * Cleanup. * Improve types and make type checking happy. * Let's see whether older Pythons barf on this. * Revert "Let's see whether older Pythons barf on this." This reverts commit9973af3dbe. * Add noqa. --------- (cherry picked from commitc7f6a28d89) Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
a2c7f9f89a
commit
377a599372
56 changed files with 725 additions and 469 deletions
|
|
@ -12,7 +12,8 @@ from ansible_collections.community.general.plugins.module_utils.python_runner im
|
|||
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from .cmd_runner_fmt import ArgFormatType
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible_collections.community.general.plugins.module_utils.cmd_runner import ArgFormatter
|
||||
|
||||
|
||||
django_std_args = dict(
|
||||
|
|
@ -36,7 +37,7 @@ _pks = dict(
|
|||
primary_keys=dict(type="list", elements="str"),
|
||||
)
|
||||
|
||||
_django_std_arg_fmts: dict[str, ArgFormatType] = dict(
|
||||
_django_std_arg_fmts: dict[str, ArgFormatter] = dict(
|
||||
all=cmd_runner_fmt.as_bool("--all"),
|
||||
app=cmd_runner_fmt.as_opt_val("--app"),
|
||||
apps=cmd_runner_fmt.as_list(),
|
||||
|
|
@ -81,7 +82,7 @@ _args_menu = dict(
|
|||
|
||||
|
||||
class _DjangoRunner(PythonRunner):
|
||||
def __init__(self, module, arg_formats=None, **kwargs):
|
||||
def __init__(self, module: AnsibleModule, arg_formats=None, **kwargs) -> None:
|
||||
arg_fmts = dict(arg_formats) if arg_formats else {}
|
||||
arg_fmts.update(_django_std_arg_fmts)
|
||||
|
||||
|
|
@ -108,12 +109,12 @@ class _DjangoRunner(PythonRunner):
|
|||
class DjangoModuleHelper(ModuleHelper):
|
||||
module = {}
|
||||
django_admin_cmd: str | None = None
|
||||
arg_formats: dict[str, ArgFormatType] = {}
|
||||
arg_formats: dict[str, ArgFormatter] = {}
|
||||
django_admin_arg_order: tuple[str, ...] | str = ()
|
||||
_django_args: list[str] = []
|
||||
_check_mode_arg: str = ""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
self.module["argument_spec"], self.arg_formats = self._build_args(
|
||||
self.module.get("argument_spec", {}), self.arg_formats, *(["std"] + self._django_args)
|
||||
)
|
||||
|
|
@ -122,9 +123,9 @@ class DjangoModuleHelper(ModuleHelper):
|
|||
self.vars.command = self.django_admin_cmd
|
||||
|
||||
@staticmethod
|
||||
def _build_args(arg_spec, arg_format, *names):
|
||||
res_arg_spec = {}
|
||||
res_arg_fmts = {}
|
||||
def _build_args(arg_spec, arg_format, *names) -> tuple[dict[str, t.Any], dict[str, ArgFormatter]]:
|
||||
res_arg_spec: dict[str, t.Any] = {}
|
||||
res_arg_fmts: dict[str, ArgFormatter] = {}
|
||||
for name in names:
|
||||
args, fmts = _args_menu[name]
|
||||
res_arg_spec = dict_merge(res_arg_spec, args)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue