-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add additional checks for match class patterns #10587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
42b289d
279e8cc
3ce613a
a8e71f1
768c4c9
5502cd3
96cf94e
9ba694b
f420f28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Book: | ||
__match_args__ = ("title", "year") | ||
|
||
def __init__(self, title, year): | ||
self.title = title | ||
self.year = year | ||
|
||
|
||
def func(item: Book): | ||
match item: | ||
case Book(title=str(title)): # [match-class-bind-self] | ||
... | ||
case Book(year=int(year)): # [match-class-bind-self] | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Book: | ||
__match_args__ = ("title", "year") | ||
|
||
def __init__(self, title, year): | ||
self.title = title | ||
self.year = year | ||
|
||
|
||
def func(item: Book): | ||
match item: | ||
case Book(title=str() as title): | ||
... | ||
case Book(year=int() as year): | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- `Python documentation <https://docs.python.org/3/reference/compound_stmts.html#class-patterns>`_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Book: | ||
__match_args__ = ("title", "year") | ||
|
||
def __init__(self, title, year): | ||
self.title = title | ||
self.year = year | ||
|
||
|
||
def func(item: Book): | ||
match item: | ||
case Book("abc", 2000): # [match-class-positional-attributes] | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Book: | ||
__match_args__ = ("title", "year") | ||
|
||
def __init__(self, title, year): | ||
self.title = title | ||
self.year = year | ||
|
||
|
||
def func(item: Book): | ||
match item: | ||
case Book(title="abc", year=2000): | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- `Python documentation <https://docs.python.org/3/reference/compound_stmts.html#class-patterns>`_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Add additional checks for suboptimal uses of class patterns in :keyword:`match`. | ||
|
||
* :ref:`match-class-bind-self` is emitted if a name is bound to ``self`` instead of | ||
using an ``as`` pattern. | ||
* :ref:`match-class-positional-attributes` is emitted if a class pattern has positional | ||
attributes when keywords could be used. | ||
|
||
Refs #10586 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,9 +327,9 @@ def nested_match_case(data): # [too-complex] | |
match data: | ||
case {"type": "user", "data": user_data}: | ||
match user_data: # Nested match adds complexity | ||
case {"name": str(name)}: | ||
case {"name": str() as name}: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check already making pylint better 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed most of the pylint code in #10580 already. That's why there are "only" two instances in a test case left here. |
||
return f"User: {name}" | ||
case {"id": int(user_id)}: | ||
case {"id": int() as user_id}: | ||
return f"User ID: {user_id}" | ||
case _: | ||
return "Unknown user format" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
invalid-match-args-definition:11:21:11:31:C:`__match_args__` must be a tuple of strings.:HIGH | ||
invalid-match-args-definition:14:21:14:29:D:`__match_args__` must be a tuple of strings.:HIGH | ||
too-many-positional-sub-patterns:25:13:25:20:f1:A expects 1 positional sub-patterns (given 2):INFERENCE | ||
too-many-positional-sub-patterns:27:13:27:23:f1:B expects 2 positional sub-patterns (given 3):INFERENCE | ||
multiple-class-sub-patterns:32:13:32:22:f2:Multiple sub-patterns for attribute x:INFERENCE | ||
multiple-class-sub-patterns:34:13:34:29:f2:Multiple sub-patterns for attribute x:INFERENCE | ||
undefined-variable:40:13:40:23:f2:Undefined variable 'NotDefined':UNDEFINED | ||
invalid-match-args-definition:14:21:14:31:C:`__match_args__` must be a tuple of strings.:HIGH | ||
invalid-match-args-definition:17:21:17:29:D:`__match_args__` must be a tuple of strings.:HIGH | ||
too-many-positional-sub-patterns:34:13:34:20:f1:A expects 1 positional sub-patterns (given 2):INFERENCE | ||
too-many-positional-sub-patterns:36:13:36:23:f1:B expects 2 positional sub-patterns (given 3):INFERENCE | ||
too-many-positional-sub-patterns:38:13:38:22:f1:int expects 1 positional sub-patterns (given 2):INFERENCE | ||
too-many-positional-sub-patterns:40:13:40:24:f1:tuple expects 1 positional sub-patterns (given 2):INFERENCE | ||
multiple-class-sub-patterns:47:13:47:22:f2:Multiple sub-patterns for attribute x:INFERENCE | ||
multiple-class-sub-patterns:49:13:49:29:f2:Multiple sub-patterns for attribute x:INFERENCE | ||
undefined-variable:55:13:55:23:f2:Undefined variable 'NotDefined':UNDEFINED | ||
match-class-bind-self:60:17:60:18:f3:Use 'int() as y' instead:HIGH | ||
match-class-bind-self:63:17:63:18:f3:Use 'str() as y' instead:HIGH | ||
match-class-positional-attributes:75:13:75:17:f4:Use keyword attributes instead of positional ones ('x'):INFERENCE | ||
match-class-positional-attributes:77:13:77:20:f4:Use keyword attributes instead of positional ones ('x', 'y'):INFERENCE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This is a suggestion, the message is short, there's room for explanation in it, maybe ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems Daniel merged it before I saw your comment 😄
Anyway, not sure we need an extra comment here. I did think about moving these checks to an extension (e.g.
CodeStyle
. However, there are clear performance benefits which is why I decided to keep them always enabled.If we get reports about them, we could still consider amending the message or moving the check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, sorry! I saw Pierre's approval and thought it was a comment from his earlier review.
Will be more careful next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, it was good to merge which is why I approved in the first place 😄