-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand RUF008 to all classes, but to a new code (RUF012) (#4390)
AFAIK, there is no reason to limit RUF008 to just dataclasses -- mutable defaults have the same problems for regular classes. Partially addresses #4053 and broken out from #4096. --------- Co-authored-by: Micha Reiser <[email protected]> Co-authored-by: Charlie Marsh <[email protected]>
- Loading branch information
1 parent
70e6c21
commit 638c18f
Showing
8 changed files
with
137 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import typing | ||
from typing import ClassVar, Sequence | ||
|
||
KNOWINGLY_MUTABLE_DEFAULT = [] | ||
|
||
|
||
class A: | ||
mutable_default: list[int] = [] | ||
immutable_annotation: typing.Sequence[int] = [] | ||
without_annotation = [] | ||
ignored_via_comment: list[int] = [] # noqa: RUF012 | ||
correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
class_variable: typing.ClassVar[list[int]] = [] | ||
|
||
|
||
class B: | ||
mutable_default: list[int] = [] | ||
immutable_annotation: Sequence[int] = [] | ||
without_annotation = [] | ||
ignored_via_comment: list[int] = [] # noqa: RUF012 | ||
correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
class_variable: ClassVar[list[int]] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF012_RUF012.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
source: crates/ruff/src/rules/ruff/mod.rs | ||
--- | ||
RUF012.py:8:34: RUF012 Do not use mutable default values for class attributes | ||
| | ||
7 | class A: | ||
8 | mutable_default: list[int] = [] | ||
| ^^ RUF012 | ||
9 | immutable_annotation: typing.Sequence[int] = [] | ||
10 | without_annotation = [] | ||
| | ||
|
||
RUF012.py:10:26: RUF012 Do not use mutable default values for class attributes | ||
| | ||
8 | mutable_default: list[int] = [] | ||
9 | immutable_annotation: typing.Sequence[int] = [] | ||
10 | without_annotation = [] | ||
| ^^ RUF012 | ||
11 | ignored_via_comment: list[int] = [] # noqa: RUF012 | ||
12 | correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
| | ||
|
||
RUF012.py:17:34: RUF012 Do not use mutable default values for class attributes | ||
| | ||
16 | class B: | ||
17 | mutable_default: list[int] = [] | ||
| ^^ RUF012 | ||
18 | immutable_annotation: Sequence[int] = [] | ||
19 | without_annotation = [] | ||
| | ||
|
||
RUF012.py:19:26: RUF012 Do not use mutable default values for class attributes | ||
| | ||
17 | mutable_default: list[int] = [] | ||
18 | immutable_annotation: Sequence[int] = [] | ||
19 | without_annotation = [] | ||
| ^^ RUF012 | ||
20 | ignored_via_comment: list[int] = [] # noqa: RUF012 | ||
21 | correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.