Skip to content

Commit

Permalink
[flake8-pie] Preserve parentheses in unnecessary-dict-kwargs (#11372
Browse files Browse the repository at this point in the history
)

## Summary

Closes #11371.
  • Loading branch information
charliermarsh authored May 11, 2024
1 parent 890cc32 commit 4b330b1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
# Duplicated key names won't be fixed, to avoid syntax errors.
abc(**{'a': b}, **{'a': c}) # PIE804
abc(a=1, **{'a': c}, **{'b': c}) # PIE804

# Some values need to be parenthesized.
abc(foo=1, **{'bar': (bar := 1)}) # PIE804
abc(foo=1, **{'bar': (yield 1)}) # PIE804
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_hash::FxHashSet;

use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::{self as ast, Expr};
use ruff_python_stdlib::identifiers::is_identifier;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -121,7 +122,19 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal
.iter()
.zip(dict.iter_values())
.map(|(kwarg, value)| {
format!("{}={}", kwarg, checker.locator().slice(value.range()))
format!(
"{}={}",
kwarg,
checker.locator().slice(
parenthesized_range(
value.into(),
dict.into(),
checker.indexer().comment_ranges(),
checker.locator().contents(),
)
.unwrap_or(value.range())
)
)
})
.join(", "),
keyword.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ PIE804.py:26:10: PIE804 Unnecessary `dict` kwargs
25 | abc(**{'a': b}, **{'a': c}) # PIE804
26 | abc(a=1, **{'a': c}, **{'b': c}) # PIE804
| ^^^^^^^^^^ PIE804
27 |
28 | # Some values need to be parenthesized.
|
= help: Remove unnecessary kwargs

Expand All @@ -175,6 +177,8 @@ PIE804.py:26:22: PIE804 [*] Unnecessary `dict` kwargs
25 | abc(**{'a': b}, **{'a': c}) # PIE804
26 | abc(a=1, **{'a': c}, **{'b': c}) # PIE804
| ^^^^^^^^^^ PIE804
27 |
28 | # Some values need to be parenthesized.
|
= help: Remove unnecessary kwargs

Expand All @@ -184,5 +188,39 @@ PIE804.py:26:22: PIE804 [*] Unnecessary `dict` kwargs
25 25 | abc(**{'a': b}, **{'a': c}) # PIE804
26 |-abc(a=1, **{'a': c}, **{'b': c}) # PIE804
26 |+abc(a=1, **{'a': c}, b=c) # PIE804
27 27 |
28 28 | # Some values need to be parenthesized.
29 29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804

PIE804.py:29:12: PIE804 [*] Unnecessary `dict` kwargs
|
28 | # Some values need to be parenthesized.
29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^^ PIE804
30 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
|
= help: Remove unnecessary kwargs

Safe fix
26 26 | abc(a=1, **{'a': c}, **{'b': c}) # PIE804
27 27 |
28 28 | # Some values need to be parenthesized.
29 |-abc(foo=1, **{'bar': (bar := 1)}) # PIE804
29 |+abc(foo=1, bar=(bar := 1)) # PIE804
30 30 | abc(foo=1, **{'bar': (yield 1)}) # PIE804

PIE804.py:30:12: PIE804 [*] Unnecessary `dict` kwargs
|
28 | # Some values need to be parenthesized.
29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
30 | abc(foo=1, **{'bar': (yield 1)}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^ PIE804
|
= help: Remove unnecessary kwargs

Safe fix
27 27 |
28 28 | # Some values need to be parenthesized.
29 29 | abc(foo=1, **{'bar': (bar := 1)}) # PIE804
30 |-abc(foo=1, **{'bar': (yield 1)}) # PIE804
30 |+abc(foo=1, bar=(yield 1)) # PIE804

0 comments on commit 4b330b1

Please sign in to comment.