1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +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:
Felix Fontein 2025-11-22 22:52:21 +01:00 committed by GitHub
parent 4517b86ed4
commit 19757b3a4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 194 additions and 96 deletions

View file

@ -5,8 +5,9 @@
from __future__ import annotations
import re
import typing as t
from collections.abc import Mapping, Set
from collections.abc import Mapping, Sequence, Set
from ansible.module_utils.common.collections import is_sequence
from ansible.utils.unsafe_proxy import (
AnsibleUnsafe,
@ -17,14 +18,54 @@ _RE_TEMPLATE_CHARS = re.compile("[{}]")
_RE_TEMPLATE_CHARS_BYTES = re.compile(b"[{}]")
def make_unsafe(value):
@t.overload
def make_unsafe(value: None) -> None: ...
@t.overload
def make_unsafe(value: Mapping) -> dict: ...
@t.overload
def make_unsafe(value: Set) -> set: ...
@t.overload
def make_unsafe(value: tuple) -> tuple: ...
@t.overload
def make_unsafe(value: list) -> list: ...
@t.overload
def make_unsafe(value: Sequence) -> Sequence: ...
@t.overload
def make_unsafe(value: str) -> str: ...
@t.overload
def make_unsafe(value: bool) -> bool: ...
@t.overload
def make_unsafe(value: int) -> int: ...
@t.overload
def make_unsafe(value: float) -> float: ...
def make_unsafe(value: t.Any) -> t.Any:
if value is None or isinstance(value, AnsibleUnsafe):
return value
if isinstance(value, Mapping):
return {make_unsafe(key): make_unsafe(val) for key, val in value.items()}
elif isinstance(value, Set):
return set(make_unsafe(elt) for elt in value)
return {make_unsafe(elt) for elt in value}
elif is_sequence(value):
return type(value)(make_unsafe(elt) for elt in value)
elif isinstance(value, bytes):