mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
gh-132449: Improve the algorithm to detect typos in keywords (#132837)
This commit is contained in:
parent
85f89cb3e6
commit
32c4bbe834
2 changed files with 13 additions and 3 deletions
|
@ -1838,6 +1838,12 @@ SyntaxError: invalid syntax. Did you mean 'if'?
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
SyntaxError: invalid syntax. Did you mean 'for'?
|
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)
|
>>> f(a=23, a=234)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
|
|
|
@ -1339,10 +1339,14 @@ class TracebackException:
|
||||||
if tokens_left_to_process < 0:
|
if tokens_left_to_process < 0:
|
||||||
break
|
break
|
||||||
# Limit the number of possible matches to try
|
# Limit the number of possible matches to try
|
||||||
matches = difflib.get_close_matches(wrong_name, keyword.kwlist, n=3)
|
max_matches = 3
|
||||||
if not matches and _suggestions is not None:
|
matches = []
|
||||||
|
if _suggestions is not None:
|
||||||
suggestion = _suggestions._generate_suggestions(keyword.kwlist, wrong_name)
|
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:
|
for suggestion in matches:
|
||||||
if not suggestion or suggestion == wrong_name:
|
if not suggestion or suggestion == wrong_name:
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue