Skip to content

Commit 768c4c9

Browse files
committed
Exclude tuple subclasses
1 parent a8e71f1 commit 768c4c9

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

pylint/checkers/match_statements_checker.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,19 @@
2020

2121

2222
# List of builtin classes which match self
23-
# Exclude `dict`, `list` and `tuple` as those could also
24-
# be matched by the mapping or sequence patterns.
2523
# https://docs.python.org/3/reference/compound_stmts.html#class-patterns
2624
MATCH_CLASS_SELF_NAMES = {
2725
"builtins.bool",
2826
"builtins.bytearray",
2927
"builtins.bytes",
30-
# "builtins.dict",
28+
"builtins.dict",
3129
"builtins.float",
3230
"builtins.frozenset",
3331
"builtins.int",
34-
# "builtins.list",
32+
"builtins.list",
3533
"builtins.set",
3634
"builtins.str",
37-
# "builtins.tuple",
35+
"builtins.tuple",
3836
}
3937

4038

@@ -191,7 +189,10 @@ def visit_matchclass(self, node: nodes.MatchClass) -> None:
191189
inferred = safe_infer(node.cls)
192190
if not (
193191
isinstance(inferred, nodes.ClassDef)
194-
and inferred.qname() in MATCH_CLASS_SELF_NAMES
192+
and (
193+
inferred.qname() in MATCH_CLASS_SELF_NAMES
194+
or "tuple" in inferred.basenames
195+
)
195196
):
196197
self.add_message(
197198
"match-class-positional-attributes",

tests/functional/m/match_class_pattern.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def f1(x):
3737
case int(1): ...
3838
case int(1, 2): ... # [too-many-positional-sub-patterns]
3939
case tuple(1): ...
40-
case tuple(1, 2): ...
40+
case tuple(1, 2): ... # [too-many-positional-sub-patterns]
4141
case tuple((1, 2)): ...
4242
case Result(1, 2): ...
4343

@@ -74,5 +74,5 @@ def f4(x):
7474
case A(x=1): ...
7575
case B(1, 2): ... # [match-class-positional-attributes]
7676
case B(x=1, y=2): ...
77-
case Result(1, 2): ... # [match-class-positional-attributes]
77+
case Result(1, 2): ...
7878
case Result(x=1, y=2): ...

tests/functional/m/match_class_pattern.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ invalid-match-args-definition:17:21:17:29:D:`__match_args__` must be a tuple of
33
too-many-positional-sub-patterns:34:13:34:20:f1:A expects 1 positional sub-patterns (given 2):INFERENCE
44
too-many-positional-sub-patterns:36:13:36:23:f1:B expects 2 positional sub-patterns (given 3):INFERENCE
55
too-many-positional-sub-patterns:38:13:38:22:f1:int expects 1 positional sub-patterns (given 2):INFERENCE
6+
too-many-positional-sub-patterns:40:13:40:24:f1:tuple expects 1 positional sub-patterns (given 2):INFERENCE
67
multiple-class-sub-patterns:47:13:47:22:f2:Multiple sub-patterns for attribute x:INFERENCE
78
multiple-class-sub-patterns:49:13:49:29:f2:Multiple sub-patterns for attribute x:INFERENCE
89
undefined-variable:55:13:55:23:f2:Undefined variable 'NotDefined':UNDEFINED
910
match-class-bind-self:60:17:60:18:f3:Use 'int() as y' instead:HIGH
1011
match-class-bind-self:63:17:63:18:f3:Use 'str() as y' instead:HIGH
1112
match-class-positional-attributes:73:13:73:17:f4:Use keyword attributes instead of positional ones:HIGH
1213
match-class-positional-attributes:75:13:75:20:f4:Use keyword attributes instead of positional ones:HIGH
13-
match-class-positional-attributes:77:13:77:25:f4:Use keyword attributes instead of positional ones:HIGH

0 commit comments

Comments
 (0)