From a460624399efb2663bf9786605d0a71788090752 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 16 Nov 2024 07:00:02 -0500 Subject: [PATCH 1/2] fix: throw meaningful error message for wrong dtype of repeated dict Fix #80. Signed-off-by: Jinzhe Zeng --- dargs/dargs.py | 7 ++++++- tests/test_checker.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/dargs/dargs.py b/dargs/dargs.py index b9a1c9b..2ab0834 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -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, " + f"requires dict but {type(value).__name__} is given", + ) if path is None: path = [self.name] sub_hook(self, value, path) diff --git a/tests/test_checker.py b/tests/test_checker.py index 92926be..f3800da 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -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( From 7b93c2a7f442830dd3fd3461a7a5b2f6d84e79a1 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 16 Nov 2024 07:03:10 -0500 Subject: [PATCH 2/2] handle path=None Signed-off-by: Jinzhe Zeng --- dargs/dargs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dargs/dargs.py b/dargs/dargs.py index 2ab0834..b590074 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -391,14 +391,14 @@ def _traverse_sub( variant_hook: HookVrntType = _DUMMYHOOK, path: list[str] | None = None, ): + 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", ) - if path is None: - path = [self.name] sub_hook(self, value, path) for subvrnt in self.sub_variants.values(): variant_hook(subvrnt, value, path)