diff --git a/dargs/dargs.py b/dargs/dargs.py index b9a1c9b..b590074 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -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) 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(