Skip to content

Commit 3a2926f

Browse files
committed
add cross language keyword hints list as a general implementation
1 parent bb0200a commit 3a2926f

3 files changed

Lines changed: 100 additions & 4 deletions

File tree

Grammar/python.gram

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,12 @@ invalid_named_expression(memo):
12871287
| a=expression ':=' expression {
12881288
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
12891289
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
1290+
| a=expression '&''&' b=expression {
1291+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1292+
a, b, "invalid syntax '&&'. Use 'and' instead.") }
1293+
| a=expression '|''|' b=expression {
1294+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1295+
a, b, "invalid syntax '||'. Use 'or' instead.") }
12901296
| a=NAME '=' b=bitwise_or !('='|':=') {
12911297
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
12921298
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {

Lib/traceback.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,18 +1485,19 @@ 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 + keyword.softkwlist + ['switch'], wrong_name)
1493+
suggestion = _suggestions._generate_suggestions(keyword.kwlist + keyword.softkwlist, wrong_name)
14901494
if suggestion:
14911495
matches.append(suggestion)
14921496
matches.extend(difflib.get_close_matches(wrong_name, keyword.kwlist, n=max_matches, cutoff=0.5))
14931497
matches = matches[:max_matches]
14941498
for suggestion in matches:
14951499
if not suggestion or suggestion == wrong_name:
14961500
continue
1497-
# semantic edge case
1498-
if suggestion == 'switch' or wrong_name == 'switch':
1499-
suggestion = 'match'
15001501
# Try to replace the token with the keyword
15011502
the_lines = error_lines.copy()
15021503
the_line = the_lines[start[0] - 1][:]
@@ -1790,6 +1791,20 @@ def print(self, *, file=None, chain=True, **kwargs):
17901791
})
17911792

17921793

1794+
# Cross-language keyword suggestions.
1795+
_CROSS_LANGUAGE_KEYWORD_HINTS = frozendict({
1796+
# C/C++ equivalents
1797+
'switch': 'match',
1798+
'delete': 'del',
1799+
# function define equivalents
1800+
'function': 'def',
1801+
'func': 'def',
1802+
# null equivalents
1803+
'NULL': 'None',
1804+
'null': 'None',
1805+
'nil': 'None',
1806+
})
1807+
17931808
def _substitution_cost(ch_a, ch_b):
17941809
if ch_a == ch_b:
17951810
return 0
@@ -1869,6 +1884,13 @@ def _get_cross_language_hint(obj, wrong_name):
18691884
return None
18701885

18711886

1887+
def _get_cross_language_keyword_hint(wrong_name):
1888+
"""Check if wrong_name is a common keyword from another language
1889+
"""
1890+
hint = _CROSS_LANGUAGE_KEYWORD_HINTS.get(wrong_name)
1891+
return hint
1892+
1893+
18721894
def _get_safe___dir__(obj):
18731895
# Use obj.__dir__() to avoid a TypeError when calling dir(obj).
18741896
# See gh-131001 and gh-139933.

Parser/parser.c

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)