Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix edge case with PIE804 #7922

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

Foo.objects.create(**{**bar}) # PIE804

foo(**{})


foo(**{**data, "foo": "buzz"})
foo(**buzz)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,20 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, expr: &Expr, kwargs

let mut diagnostic = Diagnostic::new(UnnecessaryDictKwargs, expr.range());

diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
kwargs
.iter()
.zip(values.iter())
.map(|(kwarg, value)| {
format!("{}={}", kwarg.value, checker.locator().slice(value.range()))
})
.join(", "),
kw.range(),
)));
if values.is_empty() {
diagnostic.set_fix(Fix::safe_edit(Edit::deletion(kw.start(), kw.end())));
} else {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
kwargs
.iter()
.zip(values.iter())
.map(|(kwarg, value)| {
format!("{}={}", kwarg.value, checker.locator().slice(value.range()))
})
.join(", "),
kw.range(),
)));
}

checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ PIE804.py:7:1: PIE804 [*] Unnecessary `dict` kwargs
10 10 |

PIE804.py:9:1: PIE804 [*] Unnecessary `dict` kwargs
|
7 | Foo.objects.create(**{"_id": some_id}) # PIE804
8 |
9 | Foo.objects.create(**{**bar}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PIE804
|
= help: Remove unnecessary kwargs
|
7 | Foo.objects.create(**{"_id": some_id}) # PIE804
8 |
9 | Foo.objects.create(**{**bar}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PIE804
10 |
11 | foo(**{})
|
= help: Remove unnecessary kwargs

ℹ Fix
6 6 |
Expand All @@ -95,7 +97,26 @@ PIE804.py:9:1: PIE804 [*] Unnecessary `dict` kwargs
9 |-Foo.objects.create(**{**bar}) # PIE804
9 |+Foo.objects.create(**bar) # PIE804
10 10 |
11 11 |
12 12 | foo(**{**data, "foo": "buzz"})
11 11 | foo(**{})
12 12 |

PIE804.py:11:1: PIE804 [*] Unnecessary `dict` kwargs
|
9 | Foo.objects.create(**{**bar}) # PIE804
10 |
11 | foo(**{})
| ^^^^^^^^^ PIE804
|
= help: Remove unnecessary kwargs

ℹ Fix
8 8 |
9 9 | Foo.objects.create(**{**bar}) # PIE804
10 10 |
11 |-foo(**{})
11 |+foo()
12 12 |
13 13 |
14 14 | foo(**{**data, "foo": "buzz"})


Loading