Skip to content

Commit

Permalink
fix: throw meaningful error message for wrong dtype of repeated dict (#…
Browse files Browse the repository at this point in the history
…81)

Fix #80.

---------

Signed-off-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
njzjz authored Nov 16, 2024
1 parent de0d7a1 commit 7147389
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion dargs/dargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,14 @@ def _traverse_sub(
variant_hook: HookVrntType = _DUMMYHOOK,
path: list[str] | None = None,
):
assert isinstance(value, dict)
if path is None:
path = [self.name]
if not isinstance(value, dict):
raise ArgumentTypeError(
path,
f"key `{path[-1]}` gets wrong value type, "
f"requires dict but {type(value).__name__} is given",
)
sub_hook(self, value, path)
for subvrnt in self.sub_variants.values():
variant_hook(subvrnt, value, path)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def test_sub_repeat_dict(self):
}
with self.assertRaises(ArgumentTypeError):
ca.check(err_dict2)
err_dict3 = {
"base": {
"item1": {"sub1": 10, "sub2": "hello"},
"item2": "not_a_dict_error",
}
}
with self.assertRaises(ArgumentTypeError):
ca.check(err_dict3)

def test_sub_variants(self):
ca = Argument(
Expand Down

0 comments on commit 7147389

Please sign in to comment.