Improve type hints. (#913)

This commit is contained in:
Felix Fontein
2025-06-01 21:33:20 +02:00
committed by GitHub
parent 576a06b5b2
commit f68b0d0c08
13 changed files with 95 additions and 22 deletions

View File

@@ -285,11 +285,41 @@ class ACMEClient:
key_file=key_file, key_content=key_content, passphrase=passphrase
)
@t.overload
def sign_request(
self,
*,
protected: dict[str, t.Any],
payload: str | dict[str, t.Any] | None,
payload: dict[str, t.Any] | None,
key_data: dict[str, t.Any],
encode_payload: t.Literal[True] = True,
) -> dict[str, t.Any]: ...
@t.overload
def sign_request(
self,
*,
protected: dict[str, t.Any],
payload: str | bytes | None,
key_data: dict[str, t.Any],
encode_payload: t.Literal[False],
) -> dict[str, t.Any]: ...
@t.overload
def sign_request(
self,
*,
protected: dict[str, t.Any],
payload: str | bytes | dict[str, t.Any] | None,
key_data: dict[str, t.Any],
encode_payload: bool = True,
) -> dict[str, t.Any]: ...
def sign_request(
self,
*,
protected: dict[str, t.Any],
payload: str | bytes | dict[str, t.Any] | None,
key_data: dict[str, t.Any],
encode_payload: bool = True,
) -> dict[str, t.Any]:
@@ -334,12 +364,12 @@ class ACMEClient:
def send_signed_request(
self,
url: str,
payload: str | dict[str, t.Any] | None,
payload: dict[str, t.Any] | None,
*,
key_data: dict[str, t.Any] | None = None,
jws_header: dict[str, t.Any] | None = None,
parse_json_result: t.Literal[True] = True,
encode_payload: bool = True,
encode_payload: t.Literal[True] = True,
fail_on_error: bool = True,
error_msg: str | None = None,
expected_status_codes: t.Iterable[int] | None = None,
@@ -349,12 +379,42 @@ class ACMEClient:
def send_signed_request(
self,
url: str,
payload: str | dict[str, t.Any] | None,
payload: str | bytes | None,
*,
key_data: dict[str, t.Any] | None = None,
jws_header: dict[str, t.Any] | None = None,
parse_json_result: t.Literal[True] = True,
encode_payload: t.Literal[False],
fail_on_error: bool = True,
error_msg: str | None = None,
expected_status_codes: t.Iterable[int] | None = None,
) -> tuple[dict[str, t.Any] | bytes, dict[str, t.Any]]: ...
@t.overload
def send_signed_request(
self,
url: str,
payload: dict[str, t.Any] | None,
*,
key_data: dict[str, t.Any] | None = None,
jws_header: dict[str, t.Any] | None = None,
parse_json_result: t.Literal[False],
encode_payload: bool = True,
encode_payload: t.Literal[True] = True,
fail_on_error: bool = True,
error_msg: str | None = None,
expected_status_codes: t.Iterable[int] | None = None,
) -> tuple[bytes, dict[str, t.Any]]: ...
@t.overload
def send_signed_request(
self,
url: str,
payload: str | bytes | None,
*,
key_data: dict[str, t.Any] | None = None,
jws_header: dict[str, t.Any] | None = None,
parse_json_result: t.Literal[False],
encode_payload: t.Literal[False],
fail_on_error: bool = True,
error_msg: str | None = None,
expected_status_codes: t.Iterable[int] | None = None,
@@ -363,7 +423,7 @@ class ACMEClient:
def send_signed_request(
self,
url: str,
payload: str | dict[str, t.Any] | None,
payload: str | bytes | dict[str, t.Any] | None,
*,
key_data: dict[str, t.Any] | None = None,
jws_header: dict[str, t.Any] | None = None,
@@ -404,7 +464,7 @@ class ACMEClient:
encode_payload=encode_payload,
)
self._log("signed request", data=data)
data = self.module.jsonify(data)
data_str = self.module.jsonify(data)
headers = {
"Content-Type": "application/jose+json",
@@ -412,7 +472,7 @@ class ACMEClient:
resp, info = fetch_url(
self.module,
url,
data=data,
data=data_str,
headers=headers,
method="POST",
timeout=self.request_timeout,

View File

@@ -313,6 +313,8 @@ class OpenSSLCLIBackend(CryptoBackend):
f"-{key_data['hash']}",
] + cmd_postfix
out: bytes | str
rc, out, err = self.module.run_command(
openssl_sign_cmd,
data=sign_payload,
@@ -326,7 +328,7 @@ class OpenSSLCLIBackend(CryptoBackend):
)
if key_data["type"] == "ec":
dummy, der_out, dummy = self.module.run_command(
dummy, der_out, dummy2 = self.module.run_command(
[self.openssl_binary, "asn1parse", "-inform", "DER"],
data=out,
binary_data=True,

View File

@@ -34,7 +34,7 @@ def read_file(fn: str | os.PathLike) -> bytes:
# This function was adapted from an earlier version of https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/uri.py
def write_file(
*, module: AnsibleModule, dest: str | os.PathLike, content: bytes
*, module: AnsibleModule, dest: str | os.PathLike[str], content: bytes
) -> bool:
"""
Write content to destination file dest, only if the content
@@ -79,7 +79,7 @@ def write_file(
if not os.access(dest, os.R_OK):
os.remove(tmpsrc)
raise ModuleFailException(f"Destination {dest} not readable")
checksum_dest = module.sha1(dest)
checksum_dest = module.sha1(str(dest))
else:
dirname = os.path.dirname(dest) or "."
if not os.access(dirname, os.W_OK):

View File

@@ -85,6 +85,7 @@ class AcmeCertificateBackend(CertificateBackend):
f.close()
command.extend(["--csr", tmpsrc])
else:
assert self.csr_path is not None
command.extend(["--csr", self.csr_path])
command.extend(["--acme-dir", self.challenge_path])
command.extend(["--directory-url", self.acme_directory])

View File

@@ -45,7 +45,8 @@ def restore_on_failure(
if backup_file is not None:
module.atomic_move(os.path.abspath(backup_file), os.path.abspath(path))
raise
module.add_cleanup_file(backup_file)
if backup_file is not None:
module.add_cleanup_file(backup_file)
return backup_and_restore