Skip to content

Commit

Permalink
imp(leaf.py): Add type of wrong value to error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vasenin committed Jun 29, 2022
1 parent f76eb85 commit df88398
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
10 changes: 8 additions & 2 deletions ntc/leaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ def value(self, new_value) -> None:
check_val = new_value.func if isinstance(new_value, partial) else new_value
expected_type = full_type_name(self._type)
if self._subclass and (not isinstance(check_val, type) or not issubclass(check_val, self._type)):
raise TypeMismatch(f"Subclass of type <{expected_type}> expected, but {check_val!r} found for {self}!")
raise TypeMismatch(
f"Subclass of type <{expected_type}> expected,"
f" but found {check_val!r} of type {type(check_val)} for {self}!"
)
if not self._subclass and not isinstance(check_val, self._type):
raise TypeMismatch(f"Instance of type <{expected_type}> expected, but {check_val!r} found for {self}!")
raise TypeMismatch(
f"Instance of type <{expected_type}> expected,"
f" but found {check_val!r} of type {type(check_val)} for {self}!"
)
self._value = new_value

if self._parent:
Expand Down
34 changes: 21 additions & 13 deletions tests/test_bad.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
def test_bad_type():
with pytest.raises(TypeMismatch) as excinfo:
CN.load(DATA_DIR / "bad_type.py")
assert str(excinfo.value) == "Instance of type <str> expected, but 1 found for CfgLeaf(Good) at cfg.NAME!"
assert (
str(excinfo.value)
== "Instance of type <str> expected, but found 1 of type <class 'int'> for CfgLeaf(Good) at cfg.NAME!"
)


def test_bad_attr():
Expand All @@ -26,9 +29,10 @@ def test_bad_class():
with pytest.raises(TypeMismatch) as excinfo:
CN.load(DATA_DIR / "bad_class.py")
assert re.match(
r"^Instance of type <tests.data.base_class.BaseClass> expected, "
r"but <tests.data.bad.bad_class.BadClass object at 0x[0-9a-f]+> found "
r"for CfgLeaf\(<tests.data.base_class.BaseClass object at 0x[0-9a-f]+>\) at cfg.CLASS!$",
r"^Instance of type <tests.data.base_class.BaseClass> expected,"
r" but found <tests.data.bad.bad_class.BadClass object at 0x[0-9a-f]+>"
r" of type <class 'tests.data.bad.bad_class.BadClass'>"
r" for CfgLeaf\(<tests.data.base_class.BaseClass object at 0x[0-9a-f]+>\) at cfg.CLASS!$",
str(excinfo.value),
)

Expand All @@ -37,8 +41,9 @@ def test_bad_node():
with pytest.raises((TypeMismatch, SchemaError)) as excinfo:
CN.load(DATA_DIR / "bad_node.py")
assert re.match(
r"^Instance of type <tests.data.base_class.BaseClass> expected, but "
r"<tests.data.bad.bad_node.BadClass object at 0x[0-9a-f]+> found for CfgLeaf\(None\) at cfg.CLASSES.ONE!$",
r"^Instance of type <tests.data.base_class.BaseClass> expected,"
r" but found <tests.data.bad.bad_node.BadClass object at 0x[0-9a-f]+>"
r" of type <class 'tests.data.bad.bad_node.BadClass'> for CfgLeaf\(None\) at cfg.CLASSES.ONE!$",
str(excinfo.value),
)

Expand All @@ -47,9 +52,9 @@ def test_bad_node_subclass():
with pytest.raises(TypeMismatch) as excinfo:
CN.load(DATA_DIR / "bad_node_subclass.py")
assert str(excinfo.value) == (
"Subclass of type <tests.data.base_class.BaseClass> expected, but "
"<class 'tests.data.bad.bad_node_subclass.BadSubClass'> found "
"for CfgLeaf(<class 'tests.data.base_class.BaseClass'>) at cfg.SUBCLASS!"
"Subclass of type <tests.data.base_class.BaseClass> expected,"
" but found <class 'tests.data.bad.bad_node_subclass.BadSubClass'> of type <class 'type'>"
" for CfgLeaf(<class 'tests.data.base_class.BaseClass'>) at cfg.SUBCLASS!"
)


Expand All @@ -74,17 +79,20 @@ def test_bad_node_instance():
with pytest.raises((TypeMismatch, SchemaError)) as excinfo:
CN.load(DATA_DIR / "bad_node_instance.py")
assert re.match(
r"^Subclass of type <tests.data.base_class.BaseClass> expected, "
r"but <tests.data.bad.bad_node_instance.SubClass object at 0x[0-9a-f]+> found for "
r"CfgLeaf\(None\) at cfg.SUBCLASSES.ONE!$",
r"^Subclass of type <tests.data.base_class.BaseClass> expected,"
r" but found <tests.data.bad.bad_node_instance.SubClass object at 0x[0-9a-f]+>"
r" of type <class 'tests.data.bad.bad_node_instance.SubClass'>"
r" for CfgLeaf\(None\) at cfg.SUBCLASSES.ONE!$",
str(excinfo.value),
)


def test_inheritance_changes_bad():
with pytest.raises(TypeMismatch) as excinfo:
CN.load(DATA_DIR / "inheritance_changes_bad.py")
assert str(excinfo.value) == ("Instance of type <str> expected, but 2 found for CfgLeaf(BAR) at cfg.DICT.BAR!")
assert str(excinfo.value) == (
"Instance of type <str> expected, but found 2 of type <class 'int'> for CfgLeaf(BAR) at cfg.DICT.BAR!"
)


def test_bad_inherit():
Expand Down

0 comments on commit df88398

Please sign in to comment.