1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-09 21:47:17 +00:00

[PR #11167/19757b3a backport][stable-12] Add type hints to action and test plugins and to plugin utils; fix some bugs, and improve input validation (#11191)

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.



* Extend changelog fragment.

* Move task_vars initialization up.

---------


(cherry picked from commit 19757b3a4c)

Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2025-11-23 08:36:34 +01:00 committed by GitHub
parent dbcd0dc497
commit 9cdeb5a9b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 194 additions and 96 deletions

View file

@ -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,
}