Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB180.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ def foo(self): pass
class A7(B0, abc.ABC, B1):
@abstractmethod
def foo(self): pass

# Issue 22631 reproduction
class C(
other_kwarg=1,
# comment
metaclass=abc.ABCMeta,
):
pass
24 changes: 20 additions & 4 deletions crates/ruff_linter/src/rules/refurb/rules/metaclass_abcmeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
/// ```
///
/// ## Fix safety
/// The rule's fix is unsafe if the class has base classes. This is because the base classes might
/// be validating the class's other base classes (e.g., `typing.Protocol` does this) or otherwise
/// alter runtime behavior if more base classes are added.
/// The rule's fix is unsafe if the class has base classes, or if the fix would result in the
/// deletion of comments.
///
/// If the class has base classes, inheriting from `abc.ABC` might alter the runtime behavior if
/// those base classes validate the class's other base classes (e.g., `typing.Protocol`).
///
/// ## References
/// - [Python documentation: `abc.ABC`](https://docs.python.org/3/library/abc.html#abc.ABC)
Expand Down Expand Up @@ -82,11 +84,25 @@ pub(crate) fn metaclass_abcmeta(checker: &Checker, class_def: &StmtClassDef) {
return;
}

let applicability = if class_def.bases().is_empty() {
let mut applicability = if class_def.bases().is_empty() {
Applicability::Safe
} else {
Applicability::Unsafe
};

// If the fix involves a range deletion or replacement that contains a comment, the fix is
// unsafe.
if applicability == Applicability::Safe {
let range = if position > 0 {
TextRange::new(class_def.keywords()[position - 1].end(), keyword.end())
} else {
keyword.range()
};
if checker.comment_ranges().intersects(range) {
applicability = Applicability::Unsafe;
}
}

let mut diagnostic = checker.report_diagnostic(MetaClassABCMeta, keyword.range);

diagnostic.try_set_fix(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,25 @@ help: Replace with `abc.ABC`
33 |
34 |
note: This is an unsafe fix and may change runtime behavior

FURB180 [*] Use of `metaclass=abc.ABCMeta` to define abstract base class
--> FURB180.py:64:5
|
62 | other_kwarg=1,
63 | # comment
64 | metaclass=abc.ABCMeta,
| ^^^^^^^^^^^^^^^^^^^^^
65 | ):
66 | pass
|
help: Replace with `abc.ABC`
59 |
60 | # Issue 22631 reproduction
61 | class C(
- other_kwarg=1,
- # comment
- metaclass=abc.ABCMeta,
62 + abc.ABC, other_kwarg=1,
63 | ):
64 | pass
note: This is an unsafe fix and may change runtime behavior