[syntax-errors] Duplicate attributes in match class pattern (#17186)

Summary
--

Detects duplicate attributes in a `match` class pattern:

```python
match x:
    case Class(x=1, x=2): ...
```

which are more analogous to the similar check for mapping patterns than
to the
multiple assignments rule.

I also realized that both this and the mapping check would only work on
top-level patterns, despite the possibility that they can be nested
inside other
patterns:

```python
match x:
    case [{"x": 1, "x": 2}]: ...  # false negative in the old version
```

and moved these checks into the recursive pattern visitor instead.

I also tidied up some of the names like the `multiple_case_assignment`
function
and the `MultipleCaseAssignmentVisitor`, which are now doing more than
checking
for multiple assignments.

Test Plan
--

New inline tests for both classes and mappings.
This commit is contained in:
Brent Westbrook 2025-04-03 17:55:37 -04:00 committed by GitHub
parent 6a07dd227d
commit 24b1b1d52c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 1257 additions and 69 deletions

View file

@ -0,0 +1,6 @@
match x:
case Class(x=1, x=2): ...
case [Class(x=1, x=2)]: ...
case {"x": x, "y": Foo(x=1, x=2)}: ...
case [{}, {"x": x, "y": Foo(x=1, x=2)}]: ...
case Class(x=1, d={"x": 1, "x": 2}, other=Class(x=1, x=2)): ...

View file

@ -17,3 +17,6 @@ match x:
""": 2}: ...
case {"x": 1, "x": 2, "x": 3}: ...
case {0: 1, "x": 1, 0: 2, "x": 2}: ...
case [{"x": 1, "x": 2}]: ...
case Foo(x=1, y={"x": 1, "x": 2}): ...
case [Foo(x=1), Foo(x=1, y={"x": 1, "x": 2})]: ...