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
56 changed files with 725 additions and 469 deletions

View File

@@ -10,6 +10,8 @@ import os
import random
import string
import time
import typing as t
from ansible.module_utils.urls import open_url
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.common.text.converters import to_text
@@ -17,6 +19,10 @@ from ansible.module_utils.common.text.converters import to_bytes
from urllib.error import URLError, HTTPError
from urllib.parse import urlparse
if t.TYPE_CHECKING:
from ansible.module_utils.basic import AnsibleModule
GET_HEADERS = {"accept": "application/json", "OData-Version": "4.0"}
POST_HEADERS = {"content-type": "application/json", "accept": "application/json", "OData-Version": "4.0"}
PATCH_HEADERS = {"content-type": "application/json", "accept": "application/json", "OData-Version": "4.0"}
@@ -49,15 +55,15 @@ REDFISH_COMMON_ARGUMENT_SPEC = {
class RedfishUtils:
def __init__(
self,
creds,
root_uri,
creds: dict[str, str],
root_uri: str,
timeout,
module,
module: AnsibleModule,
resource_id=None,
data_modification=False,
strip_etag_quotes=False,
ciphers=None,
):
data_modification: bool = False,
strip_etag_quotes: bool = False,
ciphers: str | None = None,
) -> None:
self.root_uri = root_uri
self.creds = creds
self.timeout = timeout
@@ -73,7 +79,7 @@ class RedfishUtils:
self.validate_certs = module.params.get("validate_certs", False)
self.ca_path = module.params.get("ca_path")
def _auth_params(self, headers):
def _auth_params(self, headers: dict[str, str]) -> tuple[str | None, str | None, bool]:
"""
Return tuple of required authentication params based on the presence
of a token in the self.creds dict. If using a token, set the
@@ -151,7 +157,7 @@ class RedfishUtils:
resp["msg"] = f"Properties in {uri} are already set"
return resp
def _request(self, uri, **kwargs):
def _request(self, uri: str, **kwargs):
kwargs.setdefault("validate_certs", self.validate_certs)
kwargs.setdefault("follow_redirects", "all")
kwargs.setdefault("use_proxy", True)
@@ -163,7 +169,9 @@ class RedfishUtils:
return resp, headers
# The following functions are to send GET/POST/PATCH/DELETE requests
def get_request(self, uri, override_headers=None, allow_no_resp=False, timeout=None):
def get_request(
self, uri: str, override_headers: dict[str, str] | None = None, allow_no_resp: bool = False, timeout=None
):
req_headers = dict(GET_HEADERS)
if override_headers:
req_headers.update(override_headers)
@@ -206,7 +214,7 @@ class RedfishUtils:
return {"ret": False, "msg": f"Failed GET request to '{uri}': '{e}'"}
return {"ret": True, "data": data, "headers": headers, "resp": resp}
def post_request(self, uri, pyld, multipart=False):
def post_request(self, uri: str, pyld, multipart: bool = False):
req_headers = dict(POST_HEADERS)
username, password, basic_auth = self._auth_params(req_headers)
try:
@@ -251,7 +259,7 @@ class RedfishUtils:
return {"ret": False, "msg": f"Failed POST request to '{uri}': '{e}'"}
return {"ret": True, "data": data, "headers": headers, "resp": resp}
def patch_request(self, uri, pyld, check_pyld=False):
def patch_request(self, uri: str, pyld, check_pyld: bool = False):
req_headers = dict(PATCH_HEADERS)
r = self.get_request(uri)
if r["ret"]:
@@ -303,7 +311,7 @@ class RedfishUtils:
return {"ret": False, "changed": False, "msg": f"Failed PATCH request to '{uri}': '{e}'"}
return {"ret": True, "changed": True, "resp": resp, "msg": f"Modified {uri}"}
def put_request(self, uri, pyld):
def put_request(self, uri: str, pyld):
req_headers = dict(PUT_HEADERS)
r = self.get_request(uri)
if r["ret"]:
@@ -341,7 +349,7 @@ class RedfishUtils:
return {"ret": False, "msg": f"Failed PUT request to '{uri}': '{e}'"}
return {"ret": True, "resp": resp}
def delete_request(self, uri, pyld=None):
def delete_request(self, uri: str, pyld=None):
req_headers = dict(DELETE_HEADERS)
username, password, basic_auth = self._auth_params(req_headers)
try: