]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
utils: type parser validator: fixed bugs with exceptions missing format strings
authorVasek Sraier <git@vakabus.cz>
Mon, 12 Apr 2021 09:36:04 +0000 (11:36 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:52 +0000 (16:17 +0200)
manager/knot_resolver_manager/utils/dataclasses_parservalidator.py
manager/knot_resolver_manager/utils/types.py

index 13bee680f1aa34c48962b5c57c7b1d7603a2e322..1732f077a539af077f4253a266460e13217009ce 100644 (file)
@@ -22,6 +22,8 @@ class ValidationException(Exception):
 
 
 def _from_dictlike_obj(cls: Any, obj: Any, default: Any, use_default: bool) -> Any:
+    # pylint: disable=too-many-branches,too-many-locals
+
     # default values
     if obj is None and use_default:
         return default
@@ -32,7 +34,7 @@ def _from_dictlike_obj(cls: Any, obj: Any, default: Any, use_default: bool) -> A
             return None
         else:
             raise ValidationException(f"Expected None, found {obj}")
-    
+
     # Union[*variants] (handles Optional[T] due to the way the typing system works)
     elif is_union(cls):
         variants = get_generic_type_arguments(cls)
@@ -41,7 +43,7 @@ def _from_dictlike_obj(cls: Any, obj: Any, default: Any, use_default: bool) -> A
                 return _from_dictlike_obj(v, obj, ..., False)
             except ValidationException:
                 pass
-        raise ValidationException("Union {cls} could not be parsed - parsing of all variants failed")
+        raise ValidationException(f"Union {cls} could not be parsed - parsing of all variants failed")
 
     # after this, there is no place for a None object
     elif obj is None:
@@ -52,7 +54,7 @@ def _from_dictlike_obj(cls: Any, obj: Any, default: Any, use_default: bool) -> A
         try:
             return cls(obj)
         except ValueError as e:
-            raise ValidationException("Failed to parse primitive type {cls}, value {obj}", e)
+            raise ValidationException(f"Failed to parse primitive type {cls}, value {obj}", e)
 
     # Literal[T]
     elif is_literal(cls):
@@ -60,7 +62,7 @@ def _from_dictlike_obj(cls: Any, obj: Any, default: Any, use_default: bool) -> A
         if obj == expected:
             return obj
         else:
-            raise ValidationException("Literal {cls} is not matched with the value {obj}")
+            raise ValidationException(f"Literal {cls} is not matched with the value {obj}")
 
     # Dict[K,V]
     elif is_dict(cls):
@@ -71,7 +73,9 @@ def _from_dictlike_obj(cls: Any, obj: Any, default: Any, use_default: bool) -> A
                 for key, val in obj.items()
             }
         except AttributeError as e:
-            raise ValidationException(f"Expected dict-like object, but failed to access its .items() method. Value was {obj}")
+            raise ValidationException(
+                f"Expected dict-like object, but failed to access its .items() method. Value was {obj}", e
+            )
 
     # List[T]
     elif is_list(cls):
index dc5d7dacc26ec856412504b37180089dfe8f2b3a..ea7b3f41d734e94079fefcd009a74ef2f01737ca 100644 (file)
@@ -50,10 +50,11 @@ def is_none_type(tp: Any) -> bool:
 
 
 class _LiteralEnum:
-    def __getitem__(self, args: Tuple[Union[str,int,bytes], ...]) -> Any:
+    def __getitem__(self, args: Tuple[Union[str, int, bytes], ...]) -> Any:
         lits = tuple(Literal[x] for x in args)
         return Union[lits]  # pyright: reportGeneralTypeIssues=false
 
+
 LiteralEnum = _LiteralEnum()