Skip to content

Commit

Permalink
feat: add did you mean message for strict check
Browse files Browse the repository at this point in the history
  • Loading branch information
njzjz authored Mar 28, 2024
1 parent 2c5e762 commit 710041d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dargs/dargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,9 @@ 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 @@ -18,8 +18,9 @@ def test_name_type(self):
# possible error
with self.assertRaises(ArgumentKeyError):
ca.check({"key2": 1})
with self.assertRaises(ArgumentKeyError):
with self.assertRaises(ArgumentKeyError) as cm:
ca.check({"key1": 1, "key2": 1}, strict=True)
self.assertIn("Did you mean: key1?", str(cm.exception))
with self.assertRaises(ArgumentTypeError):
ca.check({"key1": 1.0})
# special handle of None
Expand Down

0 comments on commit 710041d

Please sign in to comment.