Skip to content

Commit 45a0ced

Browse files
feat: add did you mean message for strict check (#54)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2c5e762 commit 45a0ced

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

dargs/dargs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,10 @@ def _check_strict(self, value: dict, path=None):
465465
return
466466
for name in value.keys():
467467
if name not in allowed_keys:
468+
dym_message = did_you_mean(name, allowed_keys)
468469
raise ArgumentKeyError(
469-
path, f"undefined key `{name}` is " "not allowed in strict mode"
470+
path,
471+
f"undefined key `{name}` is not allowed in strict mode. {dym_message}",
470472
)
471473

472474
# above are type checking part

tests/test_checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ def test_sub_fields(self):
8888
ca.check(err_dict1)
8989
err_dict1["base"]["sub2"]["subsub2"]["subsubsub1"] = 111
9090
ca.check(err_dict1) # now should pass
91-
with self.assertRaises(ArgumentKeyError):
91+
with self.assertRaises(ArgumentKeyError) as cm:
9292
ca.check(err_dict1, strict=True) # but should fail when strict
93+
self.assertIn("Did you mean: subsubsub1?", str(cm.exception))
9394
err_dict1["base"]["sub2"] = None
9495
with self.assertRaises(ArgumentTypeError):
9596
ca.check(err_dict1)

0 commit comments

Comments
 (0)