https://github.com/python/cpython/commit/32c4bbe834aeb077dd12414b65b123cf7fb5b117
commit: 32c4bbe834aeb077dd12414b65b123cf7fb5b117
branch: main
author: Pablo Galindo Salgado <pablog...@gmail.com>
committer: pablogsal <pablog...@gmail.com>
date: 2025-04-23T14:45:18+01:00
summary:

gh-132449: Improve the algorithm to detect typos in keywords (#132837)

files:
M Lib/test/test_syntax.py
M Lib/traceback.py

diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index f3abb11952dbe0..8cae62459bb675 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -1838,6 +1838,12 @@
 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 5bff101a72ca47..4b3d2b636fc6b5 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -1339,10 +1339,14 @@ def _find_keyword_typos(self):
             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

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to