Skip to content

Commit f3cdecc

Browse files
committed
focus on keyword suggestion
1 parent 27b89b6 commit f3cdecc

5 files changed

Lines changed: 16 additions & 81 deletions

File tree

Grammar/python.gram

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,12 +1287,6 @@ 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.") }
12961290
| a=NAME '=' b=bitwise_or !('='|':=') {
12971291
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
12981292
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') {

Lib/test/test_traceback.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,10 @@ 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+
("switch x:\n case:", "match"),
1819+
("delete x", "del"),
1820+
("function f():", "def"),
1821+
("func f():", "def"),
18181822
]
18191823

18201824
def test_keyword_suggestions_from_file(self):

Lib/traceback.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,14 @@ def _find_keyword_typos(self):
14931493
suggestion = _suggestions._generate_suggestions(keyword.kwlist + keyword.softkwlist, wrong_name)
14941494
if suggestion:
14951495
matches.append(suggestion)
1496-
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+
)
14971504
matches = matches[:max_matches]
14981505
for suggestion in matches:
14991506
if not suggestion or suggestion == wrong_name:
@@ -1799,10 +1806,6 @@ def print(self, *, file=None, chain=True, **kwargs):
17991806
# function define equivalents
18001807
'function': 'def',
18011808
'func': 'def',
1802-
# null equivalents
1803-
'NULL': 'None',
1804-
'null': 'None',
1805-
'nil': 'None',
18061809
})
18071810

18081811
def _substitution_cost(ch_a, ch_b):
@@ -1887,8 +1890,7 @@ def _get_cross_language_hint(obj, wrong_name):
18871890
def _get_cross_language_keyword_hint(wrong_name):
18881891
"""Check if wrong_name is a common keyword from another language
18891892
"""
1890-
hint = _CROSS_LANGUAGE_KEYWORD_HINTS.get(wrong_name)
1891-
return hint
1893+
return _CROSS_LANGUAGE_KEYWORD_HINTS.get(wrong_name)
18921894

18931895

18941896
def _get_safe___dir__(obj):
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`
2+
messages. For example, ``switch x:`` suggests ``match``, ``delete x``
3+
suggests ``del``, ``function f():`` suggests ``def``.

Parser/parser.c

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

0 commit comments

Comments
 (0)