Skip to content

Commit a8e71f1

Browse files
committed
Improve match self detection
1 parent 3ce613a commit a8e71f1

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

pylint/checkers/match_statements_checker.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@
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.
2325
# https://docs.python.org/3/reference/compound_stmts.html#class-patterns
2426
MATCH_CLASS_SELF_NAMES = {
2527
"builtins.bool",
2628
"builtins.bytearray",
2729
"builtins.bytes",
28-
"builtins.dict",
30+
# "builtins.dict",
2931
"builtins.float",
3032
"builtins.frozenset",
3133
"builtins.int",
32-
"builtins.list",
34+
# "builtins.list",
3335
"builtins.set",
3436
"builtins.str",
35-
"builtins.tuple",
37+
# "builtins.tuple",
3638
}
3739

3840

tests/functional/m/match_class_pattern.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# pylint: disable=missing-docstring,unused-variable,too-few-public-methods
22
# pylint: disable=match-class-positional-attributes
33

4+
from typing import NamedTuple
5+
46
# -- Check __match_args__ definitions --
57
class A:
68
__match_args__ = ("x",)
@@ -19,6 +21,12 @@ def f(self):
1921
__match_args__ = ["x"]
2022

2123

24+
class Result(NamedTuple):
25+
# inherits from tuple -> match self
26+
x: int
27+
y: int
28+
29+
2230
def f1(x):
2331
"""Check too many positional sub-patterns"""
2432
match x:
@@ -28,6 +36,10 @@ def f1(x):
2836
case B(1, 2, 3): ... # [too-many-positional-sub-patterns]
2937
case int(1): ...
3038
case int(1, 2): ... # [too-many-positional-sub-patterns]
39+
case tuple(1): ...
40+
case tuple(1, 2): ...
41+
case tuple((1, 2)): ...
42+
case Result(1, 2): ...
3143

3244
def f2(x):
3345
"""Check multiple sub-patterns for attribute"""
@@ -51,8 +63,6 @@ def f3(x):
5163
case str(y): ... # [match-class-bind-self]
5264
case str() as y: ...
5365
case str("Hello" as y): ...
54-
case tuple(y): ... # [match-class-bind-self]
55-
case tuple() as y: ...
5666

5767
def f4(x):
5868
"""Check for positional attributes if keywords could be used."""
@@ -64,3 +74,5 @@ def f4(x):
6474
case A(x=1): ...
6575
case B(1, 2): ... # [match-class-positional-attributes]
6676
case B(x=1, y=2): ...
77+
case Result(1, 2): ... # [match-class-positional-attributes]
78+
case Result(x=1, y=2): ...
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
invalid-match-args-definition:12:21:12:31:C:`__match_args__` must be a tuple of strings.:HIGH
2-
invalid-match-args-definition:15:21:15:29:D:`__match_args__` must be a tuple of strings.:HIGH
3-
too-many-positional-sub-patterns:26:13:26:20:f1:A expects 1 positional sub-patterns (given 2):INFERENCE
4-
too-many-positional-sub-patterns:28:13:28:23:f1:B expects 2 positional sub-patterns (given 3):INFERENCE
5-
too-many-positional-sub-patterns:30:13:30:22:f1:int expects 1 positional sub-patterns (given 2):INFERENCE
6-
multiple-class-sub-patterns:35:13:35:22:f2:Multiple sub-patterns for attribute x:INFERENCE
7-
multiple-class-sub-patterns:37:13:37:29:f2:Multiple sub-patterns for attribute x:INFERENCE
8-
undefined-variable:43:13:43:23:f2:Undefined variable 'NotDefined':UNDEFINED
9-
match-class-bind-self:48:17:48:18:f3:Use 'int() as y' instead:HIGH
10-
match-class-bind-self:51:17:51:18:f3:Use 'str() as y' instead:HIGH
11-
match-class-bind-self:54:19:54:20:f3:Use 'tuple() as y' instead:HIGH
12-
match-class-positional-attributes:63:13:63:17:f4:Use keyword attributes instead of positional ones:HIGH
13-
match-class-positional-attributes:65:13:65:20:f4:Use keyword attributes instead of positional ones:HIGH
1+
invalid-match-args-definition:14:21:14:31:C:`__match_args__` must be a tuple of strings.:HIGH
2+
invalid-match-args-definition:17:21:17:29:D:`__match_args__` must be a tuple of strings.:HIGH
3+
too-many-positional-sub-patterns:34:13:34:20:f1:A expects 1 positional sub-patterns (given 2):INFERENCE
4+
too-many-positional-sub-patterns:36:13:36:23:f1:B expects 2 positional sub-patterns (given 3):INFERENCE
5+
too-many-positional-sub-patterns:38:13:38:22:f1:int expects 1 positional sub-patterns (given 2):INFERENCE
6+
multiple-class-sub-patterns:47:13:47:22:f2:Multiple sub-patterns for attribute x:INFERENCE
7+
multiple-class-sub-patterns:49:13:49:29:f2:Multiple sub-patterns for attribute x:INFERENCE
8+
undefined-variable:55:13:55:23:f2:Undefined variable 'NotDefined':UNDEFINED
9+
match-class-bind-self:60:17:60:18:f3:Use 'int() as y' instead:HIGH
10+
match-class-bind-self:63:17:63:18:f3:Use 'str() as y' instead:HIGH
11+
match-class-positional-attributes:73:13:73:17:f4:Use keyword attributes instead of positional ones:HIGH
12+
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)