mirror of
https://github.com/astral-sh/ruff
synced 2026-01-07 14:44:17 -05:00
[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:
@@ -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)): ...
|
||||
@@ -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})]: ...
|
||||
|
||||
Reference in New Issue
Block a user