Skip to content

Commit

Permalink
feat: add did you mean message for strict check (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
njzjz and pre-commit-ci[bot] authored Mar 28, 2024
1 parent 2c5e762 commit 45a0ced
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion dargs/dargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,10 @@ def _check_strict(self, value: dict, path=None):
return
for name in value.keys():
if name not in allowed_keys:
dym_message = did_you_mean(name, allowed_keys)
raise ArgumentKeyError(
path, f"undefined key `{name}` is " "not allowed in strict mode"
path,
f"undefined key `{name}` is not allowed in strict mode. {dym_message}",
)

# above are type checking part
Expand Down
3 changes: 2 additions & 1 deletion tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ def test_sub_fields(self):
ca.check(err_dict1)
err_dict1["base"]["sub2"]["subsub2"]["subsubsub1"] = 111
ca.check(err_dict1) # now should pass
with self.assertRaises(ArgumentKeyError):
with self.assertRaises(ArgumentKeyError) as cm:
ca.check(err_dict1, strict=True) # but should fail when strict
self.assertIn("Did you mean: subsubsub1?", str(cm.exception))
err_dict1["base"]["sub2"] = None
with self.assertRaises(ArgumentTypeError):
ca.check(err_dict1)
Expand Down

0 comments on commit 45a0ced

Please sign in to comment.