From 8e9a6e30a836c32b342a1c3a79d84fc8ee3bbe19 Mon Sep 17 00:00:00 2001 From: Vasek Sraier Date: Mon, 12 Apr 2021 11:36:04 +0200 Subject: [PATCH] utils: type parser validator: fixed bugs with exceptions missing format strings --- .../utils/dataclasses_parservalidator.py | 14 +++++++++----- manager/knot_resolver_manager/utils/types.py | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/manager/knot_resolver_manager/utils/dataclasses_parservalidator.py b/manager/knot_resolver_manager/utils/dataclasses_parservalidator.py index 13bee680f..1732f077a 100644 --- a/manager/knot_resolver_manager/utils/dataclasses_parservalidator.py +++ b/manager/knot_resolver_manager/utils/dataclasses_parservalidator.py @@ -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): diff --git a/manager/knot_resolver_manager/utils/types.py b/manager/knot_resolver_manager/utils/types.py index dc5d7dacc..ea7b3f41d 100644 --- a/manager/knot_resolver_manager/utils/types.py +++ b/manager/knot_resolver_manager/utils/types.py @@ -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() -- 2.47.3