]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132449: Improve the algorithm to detect typos in keywords (#132837)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Wed, 23 Apr 2025 13:45:18 +0000 (14:45 +0100)
committerGitHub <noreply@github.com>
Wed, 23 Apr 2025 13:45:18 +0000 (14:45 +0100)
Lib/test/test_syntax.py
Lib/traceback.py

index f3abb11952dbe0afc59d47f993c432fffde5dbae..8cae62459bb675f15d29c19f74fb62ff22b5b188 100644 (file)
@@ -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):
    ...
index 5bff101a72ca47b5bc5ab69df1d37d831695c142..4b3d2b636fc6b53dcaa9d8c07adfbcc5c8ce4099 100644 (file)
@@ -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