Skip to content

Commit afa7433

Browse files
committed
Respect trailing comma in unnecessary-dict-kwargs
1 parent 268d95e commit afa7433

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE804.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
foo(**{})
1212

13-
1413
foo(**{**data, "foo": "buzz"})
1514
foo(**buzz)
1615
foo(**{"bar-foo": True})
@@ -20,3 +19,5 @@
2019
foo(**{"": True})
2120
foo(**{f"buzz__{bar}": True})
2221
abc(**{"for": 3})
22+
23+
foo(**{},)

crates/ruff_linter/src/checkers/ast/analyze/expression.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
543543
flake8_bugbear::rules::no_explicit_stacklevel(checker, call);
544544
}
545545
if checker.enabled(Rule::UnnecessaryDictKwargs) {
546-
flake8_pie::rules::unnecessary_dict_kwargs(checker, expr, keywords);
546+
flake8_pie::rules::unnecessary_dict_kwargs(checker, call);
547547
}
548548
if checker.enabled(Rule::UnnecessaryRangeStart) {
549549
flake8_pie::rules::unnecessary_range_start(checker, call);

crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use itertools::Itertools;
22
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
3-
use ruff_python_ast::{self as ast, Expr, Keyword};
3+
use ruff_python_ast::{self as ast, Expr};
44

55
use ruff_macros::{derive_message_formats, violation};
66
use ruff_text_size::Ranged;
77

88
use ruff_python_stdlib::identifiers::is_identifier;
99

1010
use crate::checkers::ast::Checker;
11+
use crate::fix::edits::{remove_argument, Parentheses};
1112

1213
/// ## What it does
1314
/// Checks for unnecessary `dict` kwargs.
@@ -52,8 +53,8 @@ impl AlwaysFixableViolation for UnnecessaryDictKwargs {
5253
}
5354

5455
/// PIE804
55-
pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, expr: &Expr, kwargs: &[Keyword]) {
56-
for kw in kwargs {
56+
pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCall) {
57+
for kw in &call.arguments.keywords {
5758
// keyword is a spread operator (indicated by None)
5859
if kw.arg.is_some() {
5960
continue;
@@ -65,7 +66,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, expr: &Expr, kwargs
6566

6667
// Ex) `foo(**{**bar})`
6768
if matches!(keys.as_slice(), [None]) {
68-
let mut diagnostic = Diagnostic::new(UnnecessaryDictKwargs, expr.range());
69+
let mut diagnostic = Diagnostic::new(UnnecessaryDictKwargs, call.range());
6970

7071
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
7172
format!("**{}", checker.locator().slice(values[0].range())),
@@ -86,10 +87,18 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, expr: &Expr, kwargs
8687
continue;
8788
}
8889

89-
let mut diagnostic = Diagnostic::new(UnnecessaryDictKwargs, expr.range());
90+
let mut diagnostic = Diagnostic::new(UnnecessaryDictKwargs, call.range());
9091

9192
if values.is_empty() {
92-
diagnostic.set_fix(Fix::safe_edit(Edit::deletion(kw.start(), kw.end())));
93+
diagnostic.try_set_fix(|| {
94+
remove_argument(
95+
kw,
96+
&call.arguments,
97+
Parentheses::Preserve,
98+
checker.locator().contents(),
99+
)
100+
.map(Fix::safe_edit)
101+
});
93102
} else {
94103
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
95104
kwargs

crates/ruff_linter/src/rules/flake8_pie/snapshots/ruff_linter__rules__flake8_pie__tests__PIE804_PIE804.py.snap

+20-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ PIE804.py:11:1: PIE804 [*] Unnecessary `dict` kwargs
106106
10 |
107107
11 | foo(**{})
108108
| ^^^^^^^^^ PIE804
109+
12 |
110+
13 | foo(**{**data, "foo": "buzz"})
109111
|
110112
= help: Remove unnecessary kwargs
111113

@@ -116,7 +118,23 @@ PIE804.py:11:1: PIE804 [*] Unnecessary `dict` kwargs
116118
11 |-foo(**{})
117119
11 |+foo()
118120
12 12 |
119-
13 13 |
120-
14 14 | foo(**{**data, "foo": "buzz"})
121+
13 13 | foo(**{**data, "foo": "buzz"})
122+
14 14 | foo(**buzz)
123+
124+
PIE804.py:23:1: PIE804 [*] Unnecessary `dict` kwargs
125+
|
126+
21 | abc(**{"for": 3})
127+
22 |
128+
23 | foo(**{},)
129+
| ^^^^^^^^^^ PIE804
130+
|
131+
= help: Remove unnecessary kwargs
132+
133+
Safe fix
134+
20 20 | foo(**{f"buzz__{bar}": True})
135+
21 21 | abc(**{"for": 3})
136+
22 22 |
137+
23 |-foo(**{},)
138+
23 |+foo()
121139

122140

0 commit comments

Comments
 (0)