Skip to content

Commit 740da9e

Browse files
author
me
committed
Remove redundant commas.
1 parent aa524c8 commit 740da9e

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

crates/ruff_linter/src/rules/refurb/rules/metaclass_abcmeta.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use itertools::Itertools;
2-
32
use ruff_diagnostics::Applicability;
43
use ruff_macros::{ViolationMetadata, derive_message_formats};
5-
use ruff_python_ast::{StmtClassDef};
4+
use ruff_python_ast::StmtClassDef;
65
use ruff_python_semantic::analyze;
7-
use ruff_text_size::{Ranged, TextRange};
6+
use ruff_text_size::Ranged;
87

98
use crate::checkers::ast::Checker;
9+
use crate::fix::edits::{Parentheses, remove_argument};
1010
use crate::importer::ImportRequest;
1111
use crate::{AlwaysFixableViolation, Edit, Fix};
1212

@@ -64,6 +64,12 @@ impl AlwaysFixableViolation for MetaClassABCMeta {
6464

6565
/// FURB180
6666
pub(crate) fn metaclass_abcmeta(checker: &Checker, class_def: &StmtClassDef) {
67+
68+
// Determine whether the class definition contains at least one attribute.
69+
let Some(arguments) = &class_def.arguments.as_ref() else {
70+
return;
71+
};
72+
6773
// Identify the `metaclass` keyword.
6874
let Some((position, keyword)) = class_def.keywords().iter().find_position(|&keyword| {
6975
keyword
@@ -105,32 +111,42 @@ pub(crate) fn metaclass_abcmeta(checker: &Checker, class_def: &StmtClassDef) {
105111
);
106112

107113
Ok(if position > 0 {
108-
// When the `abc.ABCMeta` is not the first keyword, put `abc.ABC` before the first
109-
// keyword, but only if it not already presented.
110-
let rest = if has_abc {
114+
// When the `abc.ABCMeta` is not the first keyword and `abc.ABC` is not
115+
// in base classes put `abc.ABC` before the first keyword argument.
116+
let edits = if has_abc {
111117
vec![]
112118
} else {
113119
vec![
114120
Edit::insertion(format!("{binding}, "), class_def.keywords()[0].start()),
115121
import_edit,
116122
]
117123
};
124+
118125
Fix::applicable_edits(
119-
// Delete from the previous argument, to the end of the `metaclass` argument.
120-
Edit::range_deletion(TextRange::new(
121-
class_def.keywords()[position - 1].end(),
122-
keyword.end(),
123-
)),
124-
// Insert `abc.ABC` before the first keyword if needed.
125-
rest,
126+
// Delete the `metaclass` keyword.
127+
remove_argument(
128+
keyword,
129+
arguments,
130+
Parentheses::Remove,
131+
checker.source(),
132+
checker.tokens(),
133+
)?,
134+
// Add `abc.ABC` if needed.
135+
edits,
126136
applicability,
127137
)
128138
} else {
129-
// If class already inherits the `abc.ABC` we need to delete the entire
130-
// `metaclass` argument, otherwise, replace metaclass-keyword with `abc.ABC`.
131139
let edit_action = if has_abc {
132-
Edit::range_deletion(keyword.range)
140+
// Class already inherits the `abc.ABC`, delete the `metaclass` keyword only.
141+
remove_argument(
142+
keyword,
143+
arguments,
144+
Parentheses::Remove,
145+
checker.source(),
146+
checker.tokens(),
147+
)?
133148
} else {
149+
// Replace `metaclass` keyword by `abc.ABC`.
134150
Edit::range_replacement(binding, keyword.range)
135151
};
136152

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB180_FURB180.py.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ help: Replace with `abc.ABC`
9090
60 |
9191
61 | # Regression tests for https://github.com/astral-sh/ruff/issues/17162
9292
- class A8(abc.ABC, metaclass=ABCMeta): # FURB180
93-
62 + class A8(abc.ABC, ): # FURB180
93+
62 + class A8(abc.ABC): # FURB180
9494
63 | @abstractmethod
9595
64 | def foo(self):
9696
65 | pass
@@ -111,7 +111,7 @@ help: Replace with `abc.ABC`
111111
69 | from abc import ABC
112112
70 |
113113
- class A9(ABC, metaclass=ABCMeta): # FURB180
114-
71 + class A9(ABC, ): # FURB180
114+
71 + class A9(ABC): # FURB180
115115
72 | @abstractmethod
116116
73 | def foo(self):
117117
74 | pass
@@ -132,7 +132,7 @@ help: Replace with `abc.ABC`
132132
78 | from abc import ABC as ABCAlternativeName
133133
79 |
134134
- class A10(ABCAlternativeName, metaclass=ABCMeta): # FURB180
135-
80 + class A10(ABCAlternativeName, ): # FURB180
135+
80 + class A10(ABCAlternativeName): # FURB180
136136
81 | @abstractmethod
137137
82 | def foo(self):
138138
83 | pass
@@ -151,7 +151,7 @@ help: Replace with `abc.ABC`
151151
87 |
152152
88 |
153153
- class A11(MyMetaClass, metaclass=ABCMeta): # FURB180
154-
89 + class A11(MyMetaClass, ): # FURB180
154+
89 + class A11(MyMetaClass): # FURB180
155155
90 | @abstractmethod
156156
91 | def foo(self):
157157
92 | pass

0 commit comments

Comments
 (0)