]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
utils: modelling: improved error messages
authorVasek Sraier <git@vakabus.cz>
Wed, 22 Sep 2021 08:51:10 +0000 (10:51 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:53 +0000 (16:17 +0200)
manager/knot_resolver_manager/exceptions.py
manager/knot_resolver_manager/utils/modelling.py

index 02806fc9edbf015dcae268e1b948fb67e3b5696d..ed7de7804b27bcaae929b752eb599b384c0fc4ee 100644 (file)
@@ -15,7 +15,7 @@ class TreeException(KresdManagerException):
 
     def where(self) -> str:
         return self._tree_path
-    
+
     def __str__(self) -> str:
         return super().__str__() + f" @ {self.where()}"
 
index 860ad02664543bb65aebbd5e5ee8f755430b38cb..cdd8855d25a30a25aa55ef1d81db4f0d5bb6fc2e 100644 (file)
@@ -1,5 +1,5 @@
 import inspect
-from typing import Any, Dict, List, Optional, Set, Tuple, Type, Union
+from typing import Any, Dict, Optional, Set, Tuple, Type, Union
 
 from knot_resolver_manager.exceptions import DataException, SchemaException
 from knot_resolver_manager.utils.custom_types import CustomValueType
@@ -180,17 +180,18 @@ def _validated_object_type(
 
 TSource = Union[NoneType, ParsedTree, "SchemaNode", Dict[str, Any]]
 
+
 def create_untouchable(name: str):
-    class _Untouchable(object):
-        def __init__(self) -> None:
-            super().__init__()
+    class _Untouchable:
         def __getattribute__(self, item_name: str) -> Any:
             raise RuntimeError(f"You are not supposed to access object '{name}'.")
+
         def __setattr__(self, item_name: str, value: Any) -> None:
             raise RuntimeError(f"You are not supposed to access object '{name}'.")
-        
+
     return _Untouchable()
 
+
 class SchemaNode:
     _PREVIOUS_SCHEMA: Optional[Type["SchemaNode"]] = None
 
@@ -222,6 +223,13 @@ class SchemaNode:
             if source is None:
                 self._assign_default(name, python_type, object_path)
 
+            # check for invalid configuration with both transformation function and default value
+            elif hasattr(self, f"_{name}") and hasattr(self, name):
+                raise RuntimeError(
+                    f"Field '{self.__class__.__name__}.{name}' has default value and transformation function at"
+                    " the same time. That is now allowed. Store the default in the transformation function."
+                )
+
             # there is a transformation function to create the value
             elif hasattr(self, f"_{name}"):
                 val = self._get_converted_value(name, source, object_path)