8000 gh-123562: Improve `SyntaxError` message for `case ... as a.b` (#123563) · python/cpython@9878ca3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9878ca3

Browse files
sobolevnpull[bot]
authored andcommitted
gh-123562: Improve SyntaxError message for case ... as a.b (#123563)
1 parent 774fe78 commit 9878ca3

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

Grammar/python.gram

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,9 @@ invalid_case_block:
13751375
RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
13761376
invalid_as_pattern:
13771377
| or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
1378-
| or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
1378+
| or_pattern 'as' a=expression {
1379+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
1380+
a, "cannot use %s as pattern target", _PyPegen_get_expr_name(a)) }
13791381
invalid_class_pattern:
13801382
| name_or_attr '(' a=invalid_class_argument_pattern { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
13811383
PyPegen_first_item(a, pattern_ty),

Lib/test/test_patma.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,6 +3015,13 @@ def test_multiple_assignments_to_name_in_pattern_5(self):
30153015
pass
30163016
""")
30173017

3018+
def test_multiple_assignments_to_name_in_pattern_6(self):
3019+
self.assert_syntax_error("""
3020+
match ...:
3021+
case a as a + 1: # NAME and expression with no ()
3022+
pass
3023+
""")
3024+
30183025
def test_multiple_starred_names_in_sequence_pattern_0(self):
30193026
self.assert_syntax_error("""
30203027
match ...:

Lib/test/test_syntax.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,31 @@
19321932
... case 42 as 1+2+4:
19331933
... ...
19341934
Traceback (most recent call last):
1935-
SyntaxError: invalid pattern target
1935+
SyntaxError: cannot use expression as pattern target
1936+
1937+
>>> match ...:
1938+
... case 42 as a.b:
1939+
... ...
1940+
Traceback (most recent call last):
1941+
SyntaxError: cannot use attribute as pattern target
1942+
1943+
>>> match ...:
1944+
... case 42 as (a, b):
1945+
... ...
1946+
Traceback (most recent call last):
1947+
SyntaxError: cannot use tuple as pattern target
1948+
1949+
>>> match ...:
1950+
... case 42 as (a + 1):
1951+
... ...
1952+
Traceback (most recent call last):
1953+
SyntaxError: cannot use expression as pattern target
1954+
1955+
>>> match ...:
1956+
... case (32 as x) | (42 as a()):
1957+
... ...
1958+
Traceback (most recent call last):
1959+
SyntaxError: cannot use function call as pattern target
19361960
19371961
>>> match ...:
19381962
... case Foo(z=1, y=2, x):
@@ -2817,6 +2841,22 @@ def test_except_stmt_invalid_as_expr(self):
28172841
end_offset=22 + len("obj.attr"),
28182842
)
28192843

2844+
def test_match_stmt_invalid_as_expr(self):
2845+
self._check_error(
2846+
textwrap.dedent(
2847+
"""
2848+
match 1:
2849+
case x as obj.attr:
2850+
...
2851+
"""
2852+
),
2853+
errtext="cannot use attribute as pattern target",
2854+
lineno=3,
2855+
end_lineno=3,
2856+
offset=15,
2857+
end_offset=15 + len("obj.attr"),
2858+
)
2859+
28202860

28212861
def load_tests(loader, tests, pattern):
28222862
tests.addTest(doctest.DocTestSuite())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`SyntaxError` message for using ``case ... as ...`` with not a
2+
name.

Parser/parser.c

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

0 commit comments

Comments
 (0)
0