mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-28 07:57:38 +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
|
|
@ -5,18 +5,19 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import traceback
|
||||
import typing as t
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
||||
|
||||
from urllib.parse import urljoin
|
||||
|
||||
import traceback
|
||||
if t.TYPE_CHECKING:
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
def _determine_list_all_kwargs(version) -> dict[str, t.Any]:
|
||||
def _determine_list_all_kwargs(version: str) -> dict[str, t.Any]:
|
||||
gitlab_version = LooseVersion(version)
|
||||
if gitlab_version >= LooseVersion("4.0.0"):
|
||||
# 4.0.0 removed 'as_list'
|
||||
|
|
@ -42,7 +43,7 @@ except Exception:
|
|||
list_all_kwargs = {}
|
||||
|
||||
|
||||
def auth_argument_spec(spec=None):
|
||||
def auth_argument_spec(spec: dict[str, t.Any] | None = None) -> dict[str, t.Any]:
|
||||
arg_spec = dict(
|
||||
ca_path=dict(type="str"),
|
||||
api_token=dict(type="str", no_log=True),
|
||||
|
|
@ -76,7 +77,7 @@ def find_group(gitlab_instance, identifier):
|
|||
return group
|
||||
|
||||
|
||||
def ensure_gitlab_package(module, min_version=None):
|
||||
def ensure_gitlab_package(module: AnsibleModule, min_version=None) -> None:
|
||||
if not HAS_GITLAB_PACKAGE:
|
||||
module.fail_json(
|
||||
msg=missing_required_lib("python-gitlab", url="https://python-gitlab.readthedocs.io/en/stable/"),
|
||||
|
|
@ -92,7 +93,7 @@ def ensure_gitlab_package(module, min_version=None):
|
|||
)
|
||||
|
||||
|
||||
def gitlab_authentication(module, min_version=None):
|
||||
def gitlab_authentication(module: AnsibleModule, min_version=None) -> gitlab.Gitlab:
|
||||
ensure_gitlab_package(module, min_version=min_version)
|
||||
|
||||
gitlab_url = module.params["api_url"]
|
||||
|
|
@ -121,7 +122,7 @@ def gitlab_authentication(module, min_version=None):
|
|||
private_token=gitlab_token,
|
||||
oauth_token=gitlab_oauth_token,
|
||||
job_token=gitlab_job_token,
|
||||
api_version=4,
|
||||
api_version="4",
|
||||
)
|
||||
gitlab_instance.auth()
|
||||
except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e:
|
||||
|
|
@ -137,7 +138,7 @@ def gitlab_authentication(module, min_version=None):
|
|||
return gitlab_instance
|
||||
|
||||
|
||||
def filter_returned_variables(gitlab_variables):
|
||||
def filter_returned_variables(gitlab_variables) -> list[dict[str, t.Any]]:
|
||||
# pop properties we don't know
|
||||
existing_variables = [dict(x.attributes) for x in gitlab_variables]
|
||||
KNOWN = [
|
||||
|
|
@ -158,9 +159,11 @@ def filter_returned_variables(gitlab_variables):
|
|||
return existing_variables
|
||||
|
||||
|
||||
def vars_to_variables(vars, module):
|
||||
def vars_to_variables(
|
||||
vars: dict[str, str | int | float | dict[str, t.Any]], module: AnsibleModule
|
||||
) -> list[dict[str, t.Any]]:
|
||||
# transform old vars to new variables structure
|
||||
variables = list()
|
||||
variables = []
|
||||
for item, value in vars.items():
|
||||
if isinstance(value, (str, int, float)):
|
||||
variables.append(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue