Skip to content

Commit 32c4bbe

Browse files
authored
gh-132449: Improve the algorithm to detect typos in keywords (#132837)
1 parent 85f89cb commit 32c4bbe

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Lib/test/test_syntax.py

+6
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,12 @@
18381838
Traceback (most recent call last):
18391839
SyntaxError: invalid syntax. Did you mean 'for'?
18401840
1841+
1842+
>>> for x im n:
1843+
... pass
1844+
Traceback (most recent call last):
1845+
SyntaxError: invalid syntax. Did you mean 'in'?
1846+
18411847
>>> f(a=23, a=234)
18421848
Traceback (most recent call last):
18431849
...

Lib/traceback.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1339,10 +1339,14 @@ def _find_keyword_typos(self):
13391339
if tokens_left_to_process < 0:
13401340
break
13411341
# Limit the number of possible matches to try
1342-
matches = difflib.get_close_matches(wrong_name, keyword.kwlist, n=3)
1343-
if not matches and _suggestions is not None:
1342+
max_matches = 3
1343+
matches = []
1344+
if _suggestions is not None:
13441345
suggestion = _suggestions._generate_suggestions(keyword.kwlist, wrong_name)
1345-
matches = [suggestion] if suggestion is not None else matches
1346+
if suggestion:
1347+
matches.append(suggestion)
1348+
matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist, n=max_matches, cutoff=0.5))
1349+
matches = matches[:max_matches]
13461350
for suggestion in matches:
13471351
if not suggestion or suggestion == wrong_name:
13481352
continue

0 commit comments

Comments
 (0)