-
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.
Co-authored-by: Micha Reiser <[email protected]>
- Loading branch information
1 parent
832c0fa
commit 6362880
Showing
13 changed files
with
226 additions
and
14 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,59 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum, Flag, IntEnum, IntFlag, StrEnum, ReprEnum | ||
|
||
import attr | ||
import attrs | ||
|
||
|
||
## Errors | ||
|
||
@dataclass | ||
class E(Enum): ... | ||
|
||
|
||
@dataclass # Foobar | ||
class E(Flag): ... | ||
|
||
|
||
@dataclass() | ||
class E(IntEnum): ... | ||
|
||
|
||
@dataclass() # Foobar | ||
class E(IntFlag): ... | ||
|
||
|
||
@dataclass( | ||
frozen=True | ||
) | ||
class E(StrEnum): ... | ||
|
||
|
||
@dataclass( # Foobar | ||
frozen=True | ||
) | ||
class E(ReprEnum): ... | ||
|
||
|
||
@dataclass( | ||
frozen=True | ||
) # Foobar | ||
class E(Enum): ... | ||
|
||
|
||
## No errors | ||
|
||
@attrs.define | ||
class E(Enum): ... | ||
|
||
|
||
@attrs.frozen | ||
class E(Enum): ... | ||
|
||
|
||
@attrs.mutable | ||
class E(Enum): ... | ||
|
||
|
||
@attr.s | ||
class E(Enum): ... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, ViolationMetadata}; | ||
use ruff_python_ast::StmtClassDef; | ||
use ruff_python_semantic::analyze::class::is_enumeration; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::rules::ruff::rules::helpers::{dataclass_kind, DataclassKind}; | ||
|
||
/// ## What it does | ||
/// Checks for enum classes which are also decorated with `@dataclass`. | ||
/// | ||
/// ## Why is this bad? | ||
/// Decorating an enum with `@dataclass()` does not cause any errors at runtime, | ||
/// but may cause erroneous results: | ||
/// | ||
/// ```python | ||
/// @dataclass | ||
/// class E(Enum): | ||
/// A = 1 | ||
/// B = 2 | ||
/// | ||
/// print(E.A == E.B) # True | ||
/// ``` | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```python | ||
/// from dataclasses import dataclass | ||
/// from enum import Enum | ||
/// | ||
/// | ||
/// @dataclass | ||
/// class E(Enum): ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```python | ||
/// from enum import Enum | ||
/// | ||
/// | ||
/// class E(Enum): ... | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: Enum HOWTO § Dataclass support](https://docs.python.org/3/howto/enum.html#dataclass-support) | ||
#[derive(ViolationMetadata)] | ||
pub(crate) struct DataclassEnum; | ||
|
||
impl Violation for DataclassEnum { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
"An enum class should not be decorated with `@dataclass`".to_string() | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some("Remove either `@dataclass` or `Enum`".to_string()) | ||
} | ||
} | ||
|
||
/// RUF049 | ||
pub(crate) fn dataclass_enum(checker: &mut Checker, class_def: &StmtClassDef) { | ||
let semantic = checker.semantic(); | ||
|
||
let Some((DataclassKind::Stdlib, decorator)) = dataclass_kind(class_def, semantic) else { | ||
return; | ||
}; | ||
|
||
if !is_enumeration(class_def, semantic) { | ||
return; | ||
} | ||
|
||
let diagnostic = Diagnostic::new(DataclassEnum, decorator.range); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} |
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
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
67 changes: 67 additions & 0 deletions
67
.../src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF049_RUF049.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,67 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
snapshot_kind: text | ||
--- | ||
RUF049.py:10:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
8 | ## Errors | ||
9 | | ||
10 | @dataclass | ||
| ^^^^^^^^^^ RUF049 | ||
11 | class E(Enum): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` | ||
|
||
RUF049.py:14:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
14 | @dataclass # Foobar | ||
| ^^^^^^^^^^ RUF049 | ||
15 | class E(Flag): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` | ||
|
||
RUF049.py:18:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
18 | @dataclass() | ||
| ^^^^^^^^^^^^ RUF049 | ||
19 | class E(IntEnum): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` | ||
|
||
RUF049.py:22:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
22 | @dataclass() # Foobar | ||
| ^^^^^^^^^^^^ RUF049 | ||
23 | class E(IntFlag): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` | ||
|
||
RUF049.py:26:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
26 | / @dataclass( | ||
27 | | frozen=True | ||
28 | | ) | ||
| |_^ RUF049 | ||
29 | class E(StrEnum): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` | ||
|
||
RUF049.py:32:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
32 | / @dataclass( # Foobar | ||
33 | | frozen=True | ||
34 | | ) | ||
| |_^ RUF049 | ||
35 | class E(ReprEnum): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` | ||
|
||
RUF049.py:38:1: RUF049 An enum class should not be decorated with `@dataclass` | ||
| | ||
38 | / @dataclass( | ||
39 | | frozen=True | ||
40 | | ) # Foobar | ||
| |_^ RUF049 | ||
41 | class E(Enum): ... | ||
| | ||
= help: Remove either `@dataclass` or `Enum` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.