1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-29 23:18:55 +00:00

Add basic typing for module_utils (#11222)

* Add basic typing for module_utils.

* Apply some suggestions.

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* 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.

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* 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 commit 9973af3dbe.

* Add noqa.

---------

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
Felix Fontein 2025-12-01 20:40:06 +01:00 committed by GitHub
parent fb2f34ba85
commit c7f6a28d89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 725 additions and 469 deletions

View file

@ -7,10 +7,14 @@
from __future__ import annotations
import traceback
import typing as t
from functools import wraps
from ansible.module_utils.basic import missing_required_lib
if t.TYPE_CHECKING:
from ansible.module_utils.basic import AnsibleModule
PYXCLI_INSTALLED = True
PYXCLI_IMP_ERR = None
try:
@ -49,7 +53,7 @@ def xcli_wrapper(func):
"""Catch xcli errors and return a proper message"""
@wraps(func)
def wrapper(module, *args, **kwargs):
def wrapper(module: AnsibleModule, *args, **kwargs):
try:
return func(module, *args, **kwargs)
except errors.CommandExecutionError as e:
@ -59,7 +63,7 @@ def xcli_wrapper(func):
@xcli_wrapper
def connect_ssl(module):
def connect_ssl(module: AnsibleModule):
endpoints = module.params["endpoints"]
username = module.params["username"]
password = module.params["password"]
@ -72,7 +76,7 @@ def connect_ssl(module):
module.fail_json(msg=f"Connection with Spectrum Accelerate system has failed: {e}.")
def spectrum_accelerate_spec():
def spectrum_accelerate_spec() -> dict[str, t.Any]:
"""Return arguments spec for AnsibleModule"""
return dict(
endpoints=dict(required=True),
@ -82,7 +86,7 @@ def spectrum_accelerate_spec():
@xcli_wrapper
def execute_pyxcli_command(module, xcli_command, xcli_client):
def execute_pyxcli_command(module: AnsibleModule, xcli_command, xcli_client):
pyxcli_args = build_pyxcli_command(module.params)
getattr(xcli_client.cmd, xcli_command)(**(pyxcli_args))
return True
@ -99,6 +103,6 @@ def build_pyxcli_command(fields):
return pyxcli_args
def is_pyxcli_installed(module):
def is_pyxcli_installed(module: AnsibleModule) -> None:
if not PYXCLI_INSTALLED:
module.fail_json(msg=missing_required_lib("pyxcli"), exception=PYXCLI_IMP_ERR)