Skip to content

Commit 5ea1e90

Browse files
authored
gh-151128: Improve SyntaxError message for cross language keywords (GH-151129)
1 parent e9d5280 commit 5ea1e90

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,18 @@ class TestKeywordTypoSuggestions(unittest.TestCase):
18151815
("[x for x\nin range(3)\nof x]", "if"),
18161816
("[123 fur x\nin range(3)\nif x]", "for"),
18171817
("for x im n:\n pass", "in"),
1818+
("mach x:", "match"),
1819+
("math x:", "match"),
1820+
("match 1:\n cse 1:", "case"),
1821+
("typ x = int", "type"),
1822+
("typed x = int", "type"),
1823+
("lazi import x", "lazy"),
1824+
("lezi import x", "lazy"),
1825+
("switch x:\n case:", "match"),
1826+
("delete x", "del"),
1827+
("function f():", "def"),
1828+
("func f():", "def"),
1829+
("void f():", "def"),
18181830
]
18191831

18201832
def test_keyword_suggestions_from_file(self):

Lib/traceback.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,11 +1485,22 @@ def _find_keyword_typos(self):
14851485
# Limit the number of possible matches to try
14861486
max_matches = 3
14871487
matches = []
1488+
1489+
hint = _get_cross_language_keyword_hint(wrong_name)
1490+
if hint:
1491+
matches.append(hint)
14881492
if _suggestions is not None:
1489-
suggestion = _suggestions._generate_suggestions(keyword.kwlist, wrong_name)
1493+
suggestion = _suggestions._generate_suggestions(keyword.kwlist + keyword.softkwlist, wrong_name)
14901494
if suggestion:
14911495
matches.append(suggestion)
1492-
matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist, n=max_matches, cutoff=0.5))
1496+
matches.extend(
1497+
difflib.get_close_matches(
1498+
wrong_name,
1499+
keyword.kwlist + keyword.softkwlist,
1500+
n=max_matches,
1501+
cutoff=0.5
1502+
)
1503+
)
14931504
matches = matches[:max_matches]
14941505
for suggestion in matches:
14951506
if not suggestion or suggestion == wrong_name:
@@ -1787,6 +1798,17 @@ def print(self, *, file=None, chain=True, **kwargs):
17871798
})
17881799

17891800

1801+
# Cross-language keyword suggestions.
1802+
_CROSS_LANGUAGE_KEYWORD_HINTS = frozendict({
1803+
# C/C++ equivalents
1804+
'switch': 'match',
1805+
'delete': 'del',
1806+
# function define equivalents
1807+
'function': 'def',
1808+
'func': 'def',
1809+
'void': 'def',
1810+
})
1811+
17901812
def _substitution_cost(ch_a, ch_b):
17911813
if ch_a == ch_b:
17921814
return 0
@@ -1866,6 +1888,12 @@ def _get_cross_language_hint(obj, wrong_name):
18661888
return None
18671889

18681890

1891+
def _get_cross_language_keyword_hint(wrong_name):
1892+
"""Check if wrong_name is a common keyword from another language
1893+
"""
1894+
return _CROSS_LANGUAGE_KEYWORD_HINTS.get(wrong_name)
1895+
1896+
18691897
def _get_safe___dir__(obj):
18701898
# Use obj.__dir__() to avoid a TypeError when calling dir(obj).
18711899
# See gh-131001 and gh-139933.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Cross-language keyword suggestions are now shown for :exc:`SyntaxError` messages.
2+
For example, ``switch x:`` suggests ``match``, ``delete x`` suggests ``del``,
3+
``function f():`` suggests ``def``. Contributed by Zang Langyan.

0 commit comments

Comments
 (0)