]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
types: type fixes for a new version of pyright
authorVasek Sraier <git@vakabus.cz>
Sat, 12 Jun 2021 17:45:18 +0000 (19:45 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:52 +0000 (16:17 +0200)
manager/knot_resolver_manager/__main__.py
manager/knot_resolver_manager/utils/__init__.py
manager/knot_resolver_manager/utils/types.py

index 2ec240d574e7878dc43a48ec3be5544fe41b9234..7c5ae31a93dd2fa03fd23a21213201435323fcc9 100644 (file)
@@ -7,7 +7,7 @@ import click
 from knot_resolver_manager import compat
 from knot_resolver_manager.constants import LISTEN_SOCKET_PATH, MANAGER_CONFIG_FILE
 from knot_resolver_manager.server import start_server
-from knot_resolver_manager.utils import ignore_exceptions
+from knot_resolver_manager.utils import ignore_exceptions_optional
 
 
 @click.command()
@@ -36,7 +36,7 @@ def main(listen: Optional[str], config: Optional[str]):
     if listen is None:
         unix.append(LISTEN_SOCKET_PATH)
     else:
-        port = ignore_exceptions(None, ValueError)(int)(listen)
+        port = ignore_exceptions_optional(int, None, ValueError)(int)(listen)
         if port is not None:
             tcp.append(("localhost", port))
         else:
index 2d1b9812cfa577449b4224999d90b8a42f6ede80..5dd35c224fbf3d83855ed719a7337ab0fb9f553d 100644 (file)
@@ -6,9 +6,20 @@ from .overload import Overloaded
 T = TypeVar("T")
 
 
-def ignore_exceptions(
-    default: Optional[T], *exceptions: Type[BaseException]
+def ignore_exceptions_optional(
+    _tp: Type[T], default: Optional[T], *exceptions: Type[BaseException]
 ) -> Callable[[Callable[..., Optional[T]]], Callable[..., Optional[T]]]:
+    """
+    Decorator, that wraps around a function preventing it from raising exceptions
+    and instead returning the configured default value.
+
+    :param Type[T] _tp: Return type of the function. Essentialy only a template argument for type-checking
+    :param T default: The value to return as a default
+    :param List[Type[BaseException]] exceptions: The list of exceptions to catch
+    :return: value of the decorated function, or default if exception raised
+    :rtype: T
+    """
+
     def decorator(func: Callable[..., Optional[T]]) -> Callable[..., Optional[T]]:
         def f(*nargs: Any, **nkwargs: Any) -> Optional[T]:
             try:
@@ -24,7 +35,14 @@ def ignore_exceptions(
     return decorator
 
 
+def ignore_exceptions(
+    default: T, *exceptions: Type[BaseException]
+) -> Callable[[Callable[..., Optional[T]]], Callable[..., Optional[T]]]:
+    return ignore_exceptions_optional(type(default), default, *exceptions)
+
+
 __all__ = [
+    "ignore_exceptions_optional",
     "ignore_exceptions",
     "types",
     "DataclassParserValidatorMixin",
index ea7b3f41d734e94079fefcd009a74ef2f01737ca..0929f1903c9c9dacded8d5c147bad5f4f9d486db 100644 (file)
@@ -7,7 +7,7 @@ NoneType = type(None)
 
 def is_optional(tp: Any) -> bool:
     origin = getattr(tp, "__origin__", None)
-    args = getattr(tp, "__args__", [])
+    args = get_generic_type_arguments(tp)
 
     return origin == Union and len(args) == 2 and args[1] == NoneType
 
@@ -34,7 +34,8 @@ def is_literal(tp: Any) -> bool:
 
 
 def get_generic_type_arguments(tp: Any) -> List[Any]:
-    return list(getattr(tp, "__args__", []))
+    default: List[Any] = []
+    return getattr(tp, "__args__", default)
 
 
 def get_generic_type_argument(tp: Any) -> Any: