Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1cc36f2

Browse filesBrowse files
committed
Add fallback
1 parent 47d4c25 commit 1cc36f2
Copy full SHA for 1cc36f2

File tree

Expand file treeCollapse file tree

2 files changed

+26
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-2
lines changed

‎Lib/test/test_syntax.py

Copy file name to clipboardExpand all lines: Lib/test/test_syntax.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,22 @@
18221822
Traceback (most recent call last):
18231823
SyntaxError: invalid syntax. Did you mean 'raise'?
18241824
1825+
>>> [
1826+
... x for x
1827+
... in range(3)
1828+
... of x
1829+
... ]
1830+
Traceback (most recent call last):
1831+
SyntaxError: invalid syntax. Did you mean 'if'?
1832+
1833+
>>> [
1834+
... 123 fur x
1835+
... in range(3)
1836+
... if x
1837+
... ]
1838+
Traceback (most recent call last):
1839+
SyntaxError: invalid syntax. Did you mean 'for'?
1840+
18251841
>>> f(a=23, a=234)
18261842
Traceback (most recent call last):
18271843
...

‎Lib/traceback.py

Copy file name to clipboardExpand all lines: Lib/traceback.py
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,9 +1280,13 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12801280

12811281
def _find_keyword_typos(self):
12821282
assert self._is_syntax_error
1283+
try:
1284+
import _suggestions
1285+
except ImportError:
1286+
_suggestions = None
12831287

12841288
# Only try to find keyword typos if there is no custom message
1285-
if self.msg != "invalid syntax":
1289+
if self.msg != "invalid syntax" and "Perhaps you forgot a comma" not in self.msg:
12861290
return
12871291

12881292
if not self._exc_metadata:
@@ -1336,6 +1340,9 @@ def _find_keyword_typos(self):
13361340
break
13371341
# Limit the number of possible matches to try
13381342
matches = difflib.get_close_matches(wrong_name, keyword.kwlist, n=3)
1343+
if not matches and _suggestions is not None:
1344+
suggestion = _suggestions._generate_suggestions(keyword.kwlist, wrong_name)
1345+
matches = [suggestion] if suggestion is not None else matches
13391346
for suggestion in matches:
13401347
if not suggestion or suggestion == wrong_name:
13411348
continue
@@ -1350,8 +1357,9 @@ def _find_keyword_typos(self):
13501357
# Check if it works
13511358
try:
13521359
codeop.compile_command(code, symbol="exec", flags=codeop.PyCF_ONLY_AST)
1353-
except SyntaxError as e:
1360+
except SyntaxError:
13541361
continue
1362+
13551363
# Keep token.line but handle offsets correctly
13561364
self.text = token.line
13571365
self.offset = token.start[1] + 1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.