gh-123562: Improve SyntaxError message for case ... as a.b (#123563)

This commit is contained in:
sobolevn 2024-09-02 14:11:44 +03:00 committed by GitHub
parent c3ed775899
commit 23f159ae71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 10 deletions

View file

@ -3015,6 +3015,13 @@ class TestSyntaxErrors(unittest.TestCase):
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 ...:

View file

@ -1932,7 +1932,31 @@ Corner-cases that used to crash:
... 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):
@ -2817,6 +2841,22 @@ while 1:
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())