From: Pablo Galindo Salgado Date: Wed, 23 Apr 2025 13:45:18 +0000 (+0100) Subject: gh-132449: Improve the algorithm to detect typos in keywords (#132837) X-Git-Tag: v3.14.0b1~347 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=32c4bbe834aeb077dd12414b65b123cf7fb5b117;p=thirdparty%2FPython%2Fcpython.git gh-132449: Improve the algorithm to detect typos in keywords (#132837) --- diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index f3abb11952db..8cae62459bb6 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -1838,6 +1838,12 @@ SyntaxError: invalid syntax. Did you mean 'if'? Traceback (most recent call last): SyntaxError: invalid syntax. Did you mean 'for'? + +>>> for x im n: +... pass +Traceback (most recent call last): +SyntaxError: invalid syntax. Did you mean 'in'? + >>> f(a=23, a=234) Traceback (most recent call last): ... diff --git a/Lib/traceback.py b/Lib/traceback.py index 5bff101a72ca..4b3d2b636fc6 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1339,10 +1339,14 @@ class TracebackException: if tokens_left_to_process < 0: break # Limit the number of possible matches to try - matches = difflib.get_close_matches(wrong_name, keyword.kwlist, n=3) - if not matches and _suggestions is not None: + max_matches = 3 + matches = [] + if _suggestions is not None: suggestion = _suggestions._generate_suggestions(keyword.kwlist, wrong_name) - matches = [suggestion] if suggestion is not None else matches + if suggestion: + matches.append(suggestion) + matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist, n=max_matches, cutoff=0.5)) + matches = matches[:max_matches] for suggestion in matches: if not suggestion or suggestion == wrong_name: continue