From cfc36648ed14a870634c8cfa0f44361e7bda51bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20L=C3=B6sche?= Date: Thu, 5 Oct 2023 00:26:27 +0200 Subject: [PATCH] mypy --- fixca/ca.py | 28 ++++++++++++++-------------- fixca/utils.py | 17 ++++++++--------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/fixca/ca.py b/fixca/ca.py index a2c0d98..5fc7194 100644 --- a/fixca/ca.py +++ b/fixca/ca.py @@ -184,8 +184,8 @@ def __init__( if self.mountpoint not in ("/", ""): self.config[self.mountpoint] = config - @cherrypy.expose - @cherrypy.tools.allow(methods=["GET"]) + @cherrypy.expose # type: ignore + @cherrypy.tools.allow(methods=["GET"]) # type: ignore def health(self) -> str: cherrypy.response.headers["Content-Type"] = "text/plain" unhealthy = [f"- {name}" for name, fn in self.health_conditions.items() if not fn()] @@ -197,8 +197,8 @@ def health(self) -> str: cherrypy.response.headers["Content-Type"] = "text/plain" return "not ok\r\n\r\n" + "\r\n".join(unhealthy) + "\r\n" - @cherrypy.expose - @cherrypy.tools.allow(methods=["GET"]) + @cherrypy.expose # type: ignore + @cherrypy.tools.allow(methods=["GET"]) # type: ignore def metrics(self) -> bytes: cherrypy.response.headers["Content-Type"] = CONTENT_TYPE_LATEST return generate_latest() @@ -212,8 +212,8 @@ def __init__(self, ca: CertificateAuthority, psk: str) -> None: self.config = {"/": {"tools.gzip.on": False}} PSK = self.psk - @cherrypy.expose - @cherrypy.tools.allow(methods=["GET"]) + @cherrypy.expose # type: ignore + @cherrypy.tools.allow(methods=["GET"]) # type: ignore def cert(self) -> bytes: assert self.psk is not None and self.ca.cert is not None fingerprint = cert_fingerprint(self.ca.cert) @@ -225,9 +225,9 @@ def cert(self) -> bytes: ) return cert_to_bytes(self.ca.cert) - @cherrypy.expose - @cherrypy.tools.allow(methods=["POST"]) - @cherrypy.tools.jwt_check() + @cherrypy.expose # type: ignore + @cherrypy.tools.allow(methods=["POST"]) # type: ignore + @cherrypy.tools.jwt_check() # type: ignore def sign(self) -> bytes: try: csr = load_csr_from_bytes(cherrypy.request.body.read()) @@ -244,11 +244,11 @@ def sign(self) -> bytes: cherrypy.response.headers["Content-Disposition"] = f'attachment; filename="{filename}"' return cert_to_bytes(crt) - @cherrypy.expose - @cherrypy.tools.json_out() - @cherrypy.tools.json_in() - @cherrypy.tools.allow(methods=["POST"]) - @cherrypy.tools.jwt_check() + @cherrypy.expose # type: ignore + @cherrypy.tools.json_out() # type: ignore + @cherrypy.tools.json_in() # type: ignore + @cherrypy.tools.allow(methods=["POST"]) # type: ignore + @cherrypy.tools.jwt_check() # type: ignore def generate(self) -> Dict[str, Any]: try: assert self.ca.cert is not None diff --git a/fixca/utils.py b/fixca/utils.py index c15b28b..54b5f38 100644 --- a/fixca/utils.py +++ b/fixca/utils.py @@ -1,24 +1,23 @@ import time from functools import wraps -from typing import Callable, Any, Tuple, Dict, Union, TypeVar, Type +from typing import Callable, Any, Tuple, Dict, Union def str_to_bool(s: Union[str, bool]) -> bool: return str(s).lower() in ("true", "1", "yes") -RT = TypeVar("RT") - - def memoize( - ttl: int = 60, cleanup_interval: int = 600, time_fn: Callable[[], float] = time.time -) -> Callable[[Callable[..., RT]], Callable[..., RT]]: + ttl: int = 60, + cleanup_interval: int = 600, + time_fn: Callable[[], float] = time.time, +) -> Callable[[Callable[..., Any]], Callable[..., Any]]: last_cleanup: float = 0.0 - cache: Dict[Tuple[Callable[..., RT], Tuple[Any, ...], frozenset[Tuple[str, Any]]], Tuple[RT, float]] = {} + cache: Dict[Tuple[Callable[..., Any], Tuple[Any, ...], frozenset[Tuple[str, Any]]], Tuple[Any, float]] = {} - def decorating_function(user_function: Callable[..., RT]) -> Callable[..., RT]: + def decorating_function(user_function: Callable[..., Any]) -> Callable[..., Any]: @wraps(user_function) - def wrapper(*args: Any, **kwargs: Any) -> RT: + def wrapper(*args: Any, **kwargs: Any) -> Any: now = time_fn() key = (user_function, args, frozenset(kwargs.items())) if key in cache: