10000 gh-123562: Improve `SyntaxError` message for `case ... as a.b` by sobolevn · Pull Request #123563 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,9 @@ invalid_case_block:
RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
invalid_as_pattern:
| or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
| or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
| or_pattern 'as' a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "cannot use %s as pattern target", _PyPegen_get_expr_name(a)) }
invalid_class_pattern:
| name_or_attr '(' a=invalid_class_argument_pattern { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
PyPegen_first_item(a, pattern_ty),
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,13 @@ def test_multiple_assignments_to_name_in_pattern_5(self):
pass
""")

def test_multiple_assignments_to_name_in_pattern_6(self):
self.assert_syntax_error("""
match ...:
case a as a + 1: # NAME and expression with no ()
pass
""")

def test_multiple_starred_names_in_sequence_pattern_0(self):
self.assert_syntax_error("""
match ...:
Expand Down
42 changes: 41 additions & 1 deletion Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,31 @@
... case 42 as 1+2+4:
... ...
Traceback (most recent call last):
SyntaxError: invalid pattern target
SyntaxError: cannot use expression as pattern target

>>> match ...:
... case 42 as a.b:
... ...
Traceback (most recent call last):
SyntaxError: cannot use attribute as pattern target

>>> match ...:
... case 42 as (a, b):
... ...
Traceback (most recent call last):
SyntaxError: cannot use tuple as pattern target

>>> match ...:
... case 42 as (a + 1):
... ...
Traceback (most recent call last):
SyntaxError: cannot use expression as pattern target

>>> match ...:
... case (32 as x) | (42 as a()):
... ...
Traceback (most recent call last):
SyntaxError: cannot use function call as pattern target

>>> match ...:
... case Foo(z=1, y=2, x):
Expand Down Expand Up @@ -2817,6 +2841,22 @@ def test_except_stmt_invalid_as_expr(self):
end_offset=22 + len("obj.attr"),
)

def test_match_stmt_invalid_as_expr(self):
self._check_error(
textwrap.dedent(
"""
match 1:
case x as obj.attr:
...
"""
),
errtext="cannot use attribute as pattern target",
lineno=3,
end_lineno=3,
offset=15,
end_offset=15 + len("obj.attr"),
)


def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` message for using ``case ... as ...`` with not a
name.
14 changes: 6 additions & 8 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0