Skip to content

Commit

Permalink
feat: make meaningful errors for subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Mikhaylov committed Jan 19, 2021
1 parent b843a63 commit cd57231
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ntc/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,10 @@ def _set_new(self, key: str, value: Any) -> None:
if leaf_spec.required and not value_to_set.required:
raise SchemaError(f"Leaf {value_to_set} must have required == True")
if leaf_spec.subclass != value_to_set.subclass:
raise SchemaError(f"Leaf {value_to_set} must have subclass == True")
if leaf_spec.subclass:
raise SchemaError(f"Leaf {value_to_set} must be a type (subclass == True)")
else:
raise SchemaError(f"Leaf {value_to_set} cannot be a type (subclass == False)")
if not issubclass(value_to_set.type, leaf_spec.type):
raise SchemaError(f"Required type for leaf {value_to_set} must be subclass of {leaf_spec.type}")
if not leaf_spec.required and value_to_set.value is None:
Expand Down
6 changes: 6 additions & 0 deletions tests/data/bad/bad_inherit_subclass_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from ..base_cfg import cfg
from ..base_class import BaseClass

cfg = cfg.clone()

cfg.CLASSES.ANOTHER = BaseClass
4 changes: 4 additions & 0 deletions tests/data/bad/bad_inherit_subclass_class_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ntc import CN
from tests.data.bad.bad_inherit_subclass import cfg

cfg = CN(cfg)
5 changes: 5 additions & 0 deletions tests/test_bad.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def test_bad_inherit_subclass_instance():
CN.load(DATA_DIR / "bad_inherit_subclass_instance_changes.py")


def test_bad_inherit_subclass_class():
with pytest.raises(SchemaError):
CN.load(DATA_DIR / "bad_inherit_subclass_class_changes.py")


def test_schema_freeze():
with pytest.raises(SchemaError):
CN.load(DATA_DIR / "bad_schema.py")
Expand Down

0 comments on commit cd57231

Please sign in to comment.