Skip to content

Commit 482e349

Browse files
committed
Add new checks for match class patterns
1 parent f0547a2 commit 482e349

File tree

16 files changed

+235
-4
lines changed

16 files changed

+235
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Book:
2+
__match_args__ = ["title", "year"] # [invalid-match-args-definition]
3+
4+
def __init__(self, title, year):
5+
self.title = title
6+
self.year = year
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Book:
2+
__match_args__ = ("title", "year")
3+
4+
def __init__(self, title, year):
5+
self.title = title
6+
self.year = year
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `Python documentation <https://docs.python.org/3/reference/datamodel.html#customizing-positional-arguments-in-class-pattern-matching>`_
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Book:
2+
__match_args__ = ("title", "year")
3+
4+
def __init__(self, title, year):
5+
self.title = title
6+
self.year = year
7+
8+
9+
def func(item: Book):
10+
match item:
11+
case Book("abc", title="abc"): # [multiple-class-sub-patterns]
12+
...
13+
case Book(year=2000, year=2001): # [multiple-class-sub-patterns]
14+
...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Book:
2+
__match_args__ = ("title", "year")
3+
4+
def __init__(self, title, year):
5+
self.title = title
6+
self.year = year
7+
8+
9+
def func(item: Book):
10+
match item:
11+
case Book(title="abc"):
12+
...
13+
case Book(year=2000):
14+
...
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `Python documentation <https://docs.python.org/3/reference/compound_stmts.html#class-patterns>`_
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Book:
2+
__match_args__ = ("title", "year")
3+
4+
def __init__(self, title, year, author):
5+
self.title = title
6+
self.year = year
7+
self.author = author
8+
9+
10+
def func(item: Book):
11+
match item:
12+
case Book("title", 2000, "author"): # [too-many-positional-sub-patterns]
13+
...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Book:
2+
__match_args__ = ("title", "year")
3+
4+
def __init__(self, title, year, author):
5+
self.title = title
6+
self.year = year
7+
self.author = author
8+
9+
10+
def func(item: Book):
11+
match item:
12+
case Book("title", 2000, author="author"):
13+
...
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `Python documentation <https://docs.python.org/3/reference/compound_stmts.html#class-patterns>`_

doc/data/ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ line-length = 103
44

55
extend-exclude = [
66
"messages/d/duplicate-argument-name/bad.py",
7+
"messages/m/multiple-class-sub-patterns/bad.py",
78
"messages/s/syntax-error/bad.py",
89
# syntax error in newer python versions
910
"messages/b/bare-name-capture-pattern/bad.py",

0 commit comments

Comments
 (0)