Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw meaningful error message for wrong dtype of repeated dict #81

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading