mirror of https://github.com/astral-sh/ruff
[`flake8-pie`] Preserve parentheses in `unnecessary-dict-kwargs` (#11372)
## Summary Closes https://github.com/astral-sh/ruff/issues/11371.
This commit is contained in:
parent
890cc325d5
commit
4b330b11c6
|
|
@ -24,3 +24,7 @@ foo(**{},)
|
||||||
# Duplicated key names won't be fixed, to avoid syntax errors.
|
# Duplicated key names won't be fixed, to avoid syntax errors.
|
||||||
abc(**{'a': b}, **{'a': c}) # PIE804
|
abc(**{'a': b}, **{'a': c}) # PIE804
|
||||||
abc(a=1, **{'a': c}, **{'b': 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
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
|
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
|
||||||
use ruff_macros::{derive_message_formats, 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_ast::{self as ast, Expr};
|
||||||
use ruff_python_stdlib::identifiers::is_identifier;
|
use ruff_python_stdlib::identifiers::is_identifier;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
@ -121,7 +122,19 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal
|
||||||
.iter()
|
.iter()
|
||||||
.zip(dict.iter_values())
|
.zip(dict.iter_values())
|
||||||
.map(|(kwarg, value)| {
|
.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(", "),
|
.join(", "),
|
||||||
keyword.range(),
|
keyword.range(),
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,8 @@ PIE804.py:26:10: PIE804 Unnecessary `dict` kwargs
|
||||||
25 | abc(**{'a': b}, **{'a': c}) # PIE804
|
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
|
||||||
| ^^^^^^^^^^ PIE804
|
| ^^^^^^^^^^ PIE804
|
||||||
|
27 |
|
||||||
|
28 | # Some values need to be parenthesized.
|
||||||
|
|
|
|
||||||
= help: Remove unnecessary kwargs
|
= help: Remove unnecessary kwargs
|
||||||
|
|
||||||
|
|
@ -175,6 +177,8 @@ PIE804.py:26:22: PIE804 [*] Unnecessary `dict` kwargs
|
||||||
25 | abc(**{'a': b}, **{'a': c}) # PIE804
|
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
|
||||||
| ^^^^^^^^^^ PIE804
|
| ^^^^^^^^^^ PIE804
|
||||||
|
27 |
|
||||||
|
28 | # Some values need to be parenthesized.
|
||||||
|
|
|
|
||||||
= help: Remove unnecessary kwargs
|
= help: Remove unnecessary kwargs
|
||||||
|
|
||||||
|
|
@ -184,5 +188,39 @@ PIE804.py:26:22: PIE804 [*] Unnecessary `dict` kwargs
|
||||||
25 25 | abc(**{'a': b}, **{'a': c}) # PIE804
|
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
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue