From 45a0ced924ec232502c1ccff510af868a1ea4545 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 27 Mar 2024 21:17:41 -0400 Subject: [PATCH] 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> --- dargs/dargs.py | 4 +++- tests/test_checker.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dargs/dargs.py b/dargs/dargs.py index 482b512..ad4875e 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -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 diff --git a/tests/test_checker.py b/tests/test_checker.py index 398215b..2370591 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -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)