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
Fix #80.

Signed-off-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
njzjz committed Nov 16, 2024
1 parent de0d7a1 commit a460624
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,7 +391,12 @@ def _traverse_sub(
variant_hook: HookVrntType = _DUMMYHOOK,
path: list[str] | None = None,
):
assert isinstance(value, dict)
if not isinstance(value, dict):
raise ArgumentTypeError(
path,
f"key `{path[-1]}` gets wrong value type, "

Check failure on line 397 in dargs/dargs.py

View workflow job for this annotation

GitHub Actions / type_check

Object of type "None" is not subscriptable (reportOptionalSubscript)
f"requires dict but {type(value).__name__} is given",
)
if path is None:
path = [self.name]
sub_hook(self, 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 a460624

Please sign in to comment.