Skip to content

Commit

Permalink
Per review
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Jan 6, 2025
1 parent 2aaba5d commit 3d4722d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 96 deletions.
21 changes: 8 additions & 13 deletions crates/ruff_linter/src/rules/ruff/rules/dataclass_enum.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::checkers::ast::Checker;
use crate::rules::ruff::rules::helpers::{dataclass_kind, DataclassKind};
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
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 ruff_source_file::LineRanges;
use ruff_text_size::Ranged;

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`.
Expand Down Expand Up @@ -48,14 +47,14 @@ use ruff_text_size::Ranged;
#[derive(ViolationMetadata)]
pub(crate) struct DataclassEnum;

impl AlwaysFixableViolation for 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) -> String {
"Remove `@dataclass`".to_string()
fn fix_title(&self) -> Option<String> {
Some("Remove either `@dataclass` or `Enum`".to_string())
}
}

Expand All @@ -71,11 +70,7 @@ pub(crate) fn dataclass_enum(checker: &mut Checker, class_def: &StmtClassDef) {
return;
}

let decorator_last_line_full_end = checker.source().full_line_end(decorator.end());
let edit = Edit::deletion(decorator.start(), decorator_last_line_full_end);
let fix = Fix::unsafe_edit(edit);

let diagnostic = Diagnostic::new(DataclassEnum, decorator.range);

checker.diagnostics.push(diagnostic.with_fix(fix));
checker.diagnostics.push(diagnostic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,66 @@
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`
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 `@dataclass`
= help: Remove either `@dataclass` or `Enum`

ℹ Unsafe fix
7 7 |
8 8 | ## Errors
9 9 |
10 |-@dataclass
11 10 | class E(Enum): ...
12 11 |
13 12 |

RUF049.py:14:1: RUF049 [*] An enum class should not be decorated with `@dataclass`
RUF049.py:14:1: RUF049 An enum class should not be decorated with `@dataclass`
|
14 | @dataclass # Foobar
| ^^^^^^^^^^ RUF049
15 | class E(Flag): ...
|
= help: Remove `@dataclass`

ℹ Unsafe fix
11 11 | class E(Enum): ...
12 12 |
13 13 |
14 |-@dataclass # Foobar
15 14 | class E(Flag): ...
16 15 |
17 16 |
= help: Remove either `@dataclass` or `Enum`

RUF049.py:18:1: RUF049 [*] An enum class should not be decorated with `@dataclass`
RUF049.py:18:1: RUF049 An enum class should not be decorated with `@dataclass`
|
18 | @dataclass()
| ^^^^^^^^^^^^ RUF049
19 | class E(IntEnum): ...
|
= help: Remove `@dataclass`
= help: Remove either `@dataclass` or `Enum`

ℹ Unsafe fix
15 15 | class E(Flag): ...
16 16 |
17 17 |
18 |-@dataclass()
19 18 | class E(IntEnum): ...
20 19 |
21 20 |

RUF049.py:22:1: RUF049 [*] An enum class should not be decorated with `@dataclass`
RUF049.py:22:1: RUF049 An enum class should not be decorated with `@dataclass`
|
22 | @dataclass() # Foobar
| ^^^^^^^^^^^^ RUF049
23 | class E(IntFlag): ...
|
= help: Remove `@dataclass`

ℹ Unsafe fix
19 19 | class E(IntEnum): ...
20 20 |
21 21 |
22 |-@dataclass() # Foobar
23 22 | class E(IntFlag): ...
24 23 |
25 24 |
= help: Remove either `@dataclass` or `Enum`

RUF049.py:26:1: RUF049 [*] An enum class should not be decorated with `@dataclass`
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 `@dataclass`
= help: Remove either `@dataclass` or `Enum`

ℹ Unsafe fix
23 23 | class E(IntFlag): ...
24 24 |
25 25 |
26 |-@dataclass(
27 |- frozen=True
28 |-)
29 26 | class E(StrEnum): ...
30 27 |
31 28 |

RUF049.py:32:1: RUF049 [*] An enum class should not be decorated with `@dataclass`
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 `@dataclass`

ℹ Unsafe fix
29 29 | class E(StrEnum): ...
30 30 |
31 31 |
32 |-@dataclass( # Foobar
33 |- frozen=True
34 |-)
35 32 | class E(ReprEnum): ...
36 33 |
37 34 |
= help: Remove either `@dataclass` or `Enum`

RUF049.py:38:1: RUF049 [*] An enum class should not be decorated with `@dataclass`
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 `@dataclass`

ℹ Unsafe fix
35 35 | class E(ReprEnum): ...
36 36 |
37 37 |
38 |-@dataclass(
39 |- frozen=True
40 |-) # Foobar
41 38 | class E(Enum): ...
42 39 |
43 40 |
= help: Remove either `@dataclass` or `Enum`

0 comments on commit 3d4722d

Please sign in to comment.