mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-31 07:38:00 +00: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:
parent
6a07dd227d
commit
24b1b1d52c
6 changed files with 1257 additions and 69 deletions
|
@ -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})]: ...
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue