From 710041d545a28568f415e5a2c2044c0c5e31f395 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 27 Mar 2024 20:03:15 -0400 Subject: [PATCH] feat: add did you mean message for strict check --- dargs/dargs.py | 3 ++- tests/test_checker.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dargs/dargs.py b/dargs/dargs.py index 482b512..ee6faee 100644 --- a/dargs/dargs.py +++ b/dargs/dargs.py @@ -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 diff --git a/tests/test_checker.py b/tests/test_checker.py index 398215b..8ab1c31 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -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