mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-19 18:31:33 +00:00
Add type hints to action and test plugins and to plugin utils; fix some bugs, and improve input validation (#11167)
* Add type hints to action and test plugins and to plugin utils. Also fix some bugs and add proper input validation. * Combine lines. Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Extend changelog fragment. * Move task_vars initialization up. --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
4517b86ed4
commit
19757b3a4c
11 changed files with 194 additions and 96 deletions
|
|
@ -38,7 +38,11 @@ _value:
|
|||
type: boolean
|
||||
"""
|
||||
|
||||
import typing as t
|
||||
from collections.abc import Callable
|
||||
|
||||
from ansible.plugins.loader import action_loader, module_loader
|
||||
from ansible.errors import AnsibleFilterError
|
||||
|
||||
try:
|
||||
from ansible.errors import AnsiblePluginRemovedError
|
||||
|
|
@ -46,12 +50,14 @@ except ImportError:
|
|||
AnsiblePluginRemovedError = Exception # type: ignore
|
||||
|
||||
|
||||
def a_module(term):
|
||||
def a_module(term: t.Any) -> bool:
|
||||
"""
|
||||
Example:
|
||||
- 'community.general.ufw' is community.general.a_module
|
||||
- 'community.general.does_not_exist' is not community.general.a_module
|
||||
"""
|
||||
if not isinstance(term, str):
|
||||
raise AnsibleFilterError(f"Parameter must be a string, got {term!r} of type {type(term)}")
|
||||
try:
|
||||
for loader in (action_loader, module_loader):
|
||||
data = loader.find_plugin(term)
|
||||
|
|
@ -65,7 +71,7 @@ def a_module(term):
|
|||
class TestModule:
|
||||
"""Ansible jinja2 tests"""
|
||||
|
||||
def tests(self):
|
||||
def tests(self) -> dict[str, Callable]:
|
||||
return {
|
||||
"a_module": a_module,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue