Skip to content

Commit

Permalink
🏷️ Improve type hints for decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Mar 1, 2024
1 parent a01d70d commit 98e8883
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion simple_certmanager/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
return True


def _delete_obj_files(fields: List[str], obj: models.Model) -> None:
def _delete_obj_files(fields: list[str], obj: models.Model) -> None:
for name in fields:
filefield = getattr(obj, name)
with log_failed_deletes(filefield):
Expand Down
12 changes: 8 additions & 4 deletions simple_certmanager/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from functools import wraps
from typing import Any, Optional, Union
from typing import Callable, ParamSpec, TypeVar

from cryptography import x509
from cryptography.hazmat.primitives.serialization import load_pem_private_key
Expand All @@ -19,7 +19,7 @@ def load_pem_x509_private_key(data: bytes):
return load_pem_private_key(data, password=None)


def _decode(value: Union[str, bytes]) -> str:
def _decode(value: str | bytes) -> str:
# attr.value can be bytes, in which case it is must be an UTF8String or
# PrintableString (the latter being a subset of ASCII, thus also a subset of UTF8)
# See https://www.rfc-editor.org/rfc/rfc5280.txt
Expand All @@ -35,13 +35,17 @@ def pretty_print_certificate_components(x509name: x509.Name) -> str:
return ", ".join(bits)


def suppress_cryptography_errors(func):
T = TypeVar("T")
P = ParamSpec("P")


def suppress_cryptography_errors(func: Callable[P, T]) -> Callable[P, T | None]:
"""
Decorator to suppress exceptions thrown while processing PKI data.
"""

@wraps(func)
def wrapper(*args, **kwargs) -> Optional[Any]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T | None:
try:
return func(*args, **kwargs)
except ValueError as exc:
Expand Down

0 comments on commit 98e8883

Please sign in to comment.