Skip to content

Commit 7147389

Browse files
authored
fix: throw meaningful error message for wrong dtype of repeated dict (#81)
Fix #80. --------- Signed-off-by: Jinzhe Zeng <[email protected]>
1 parent de0d7a1 commit 7147389

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

dargs/dargs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,14 @@ def _traverse_sub(
391391
variant_hook: HookVrntType = _DUMMYHOOK,
392392
path: list[str] | None = None,
393393
):
394-
assert isinstance(value, dict)
395394
if path is None:
396395
path = [self.name]
396+
if not isinstance(value, dict):
397+
raise ArgumentTypeError(
398+
path,
399+
f"key `{path[-1]}` gets wrong value type, "
400+
f"requires dict but {type(value).__name__} is given",
401+
)
397402
sub_hook(self, value, path)
398403
for subvrnt in self.sub_variants.values():
399404
variant_hook(subvrnt, value, path)

tests/test_checker.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ def test_sub_repeat_dict(self):
156156
}
157157
with self.assertRaises(ArgumentTypeError):
158158
ca.check(err_dict2)
159+
err_dict3 = {
160+
"base": {
161+
"item1": {"sub1": 10, "sub2": "hello"},
162+
"item2": "not_a_dict_error",
163+
}
164+
}
165+
with self.assertRaises(ArgumentTypeError):
166+
ca.check(err_dict3)
159167

160168
def test_sub_variants(self):
161169
ca = Argument(

0 commit comments

Comments
 (0)