mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-07 20:28:56 +00:00
[syntax-errors] Fix multiple assignment for class keyword argument (#17184)
Summary -- Fixes #17181. The cases being tested with multiple *keys* being equal are actually a slightly different error, more like the error for `MatchMapping` than like the other multiple assignment errors: ```pycon >>> match x: ... case Class(x=x, x=x): ... ... File "<python-input-249>", line 2 case Class(x=x, x=x): ... ^ SyntaxError: attribute name repeated in class pattern: x >>> match x: ... case {"x": 1, "x": 2}: ... ... File "<python-input-251>", line 2 case {"x": 1, "x": 2}: ... ^^^^^^^^^^^^^^^^ SyntaxError: mapping pattern checks duplicate key ('x') >>> match x: ... case [x, x]: ... ... File "<python-input-252>", line 2 case [x, x]: ... ^ SyntaxError: multiple assignments to name 'x' in pattern ``` This PR just stops the false positive reported in the issue, but I will quickly follow it up with a new rule (or possibly combined with the mapping rule) catching the repeated attributes separately. Test Plan -- New inline `test_ok` and updating the `test_err` cases to have duplicate values instead of keys.
This commit is contained in:
parent
8aa5686e63
commit
6a07dd227d
5 changed files with 132 additions and 48 deletions
|
@ -794,6 +794,10 @@ struct MultipleCaseAssignmentVisitor<'a, Ctx> {
|
|||
|
||||
impl<'a, Ctx: SemanticSyntaxContext> MultipleCaseAssignmentVisitor<'a, Ctx> {
|
||||
fn visit_pattern(&mut self, pattern: &'a Pattern) {
|
||||
// test_ok class_keyword_in_case_pattern
|
||||
// match 2:
|
||||
// case Class(x=x): ...
|
||||
|
||||
// test_err multiple_assignment_in_case_pattern
|
||||
// match 2:
|
||||
// case [y, z, y]: ... # MatchSequence
|
||||
|
@ -802,8 +806,8 @@ impl<'a, Ctx: SemanticSyntaxContext> MultipleCaseAssignmentVisitor<'a, Ctx> {
|
|||
// case {1: x, 2: x}: ... # MatchMapping duplicate pattern
|
||||
// case {1: x, **x}: ... # MatchMapping duplicate in **rest
|
||||
// case Class(x, x): ... # MatchClass positional
|
||||
// case Class(x=1, x=2): ... # MatchClass keyword
|
||||
// case [x] | {1: x} | Class(x=1, x=2): ... # MatchOr
|
||||
// case Class(y=x, z=x): ... # MatchClass keyword
|
||||
// case [x] | {1: x} | Class(y=x, z=x): ... # MatchOr
|
||||
// case x as x: ... # MatchAs
|
||||
match pattern {
|
||||
Pattern::MatchValue(_) | Pattern::MatchSingleton(_) => {}
|
||||
|
@ -830,7 +834,6 @@ impl<'a, Ctx: SemanticSyntaxContext> MultipleCaseAssignmentVisitor<'a, Ctx> {
|
|||
self.visit_pattern(pattern);
|
||||
}
|
||||
for keyword in &arguments.keywords {
|
||||
self.insert(&keyword.attr);
|
||||
self.visit_pattern(&keyword.pattern);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue