From 02544c3217ba3b9ad132a8ff7fb6d9c782427b47 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 11 May 2024 17:53:46 -0400 Subject: [PATCH] Preserve parentheses in unnecessary-dict-kwargs --- .../test/fixtures/flake8_pie/PIE804.py | 4 ++ .../rules/unnecessary_dict_kwargs.rs | 15 +++++++- ...__flake8_pie__tests__PIE804_PIE804.py.snap | 38 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE804.py b/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE804.py index a5b3674422e1a..a8233d2917f92 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE804.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_pie/PIE804.py @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs index 81f34b0b18710..61aa28988a397 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs +++ b/crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_dict_kwargs.rs @@ -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; @@ -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(), diff --git a/crates/ruff_linter/src/rules/flake8_pie/snapshots/ruff_linter__rules__flake8_pie__tests__PIE804_PIE804.py.snap b/crates/ruff_linter/src/rules/flake8_pie/snapshots/ruff_linter__rules__flake8_pie__tests__PIE804_PIE804.py.snap index 15d993a0befee..b6a65ca50d257 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/snapshots/ruff_linter__rules__flake8_pie__tests__PIE804_PIE804.py.snap +++ b/crates/ruff_linter/src/rules/flake8_pie/snapshots/ruff_linter__rules__flake8_pie__tests__PIE804_PIE804.py.snap @@ -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 @@ -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 @@ -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