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

PIE804: Prevent keyword arguments duplication #8450

Merged
merged 9 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -20,3 +20,6 @@
foo(**{"": True})
foo(**{f"buzz__{bar}": True})
abc(**{"for": 3})

# Duplicated key names wont be fixed to avoid syntax error.
abc(**{'a': b}, **{'a': c}) # PIE804
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this work with positional arguments? For example,

def abc(a):
	pass

abc(1, **{'a': 2})

Correct me if I'm wrong but I guess it would fix it to abc(1, a=2) which isn't a syntax error although it's a runtime error.

I don't think this is in scope for this rule so maybe we could just highlight it in the docs. I'll leave this up to @zanieb as the main reviewer :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, needs info in docs, with considering bellow code as correct:

def abc(a, /, **kw): ...

abc(1, **{'a': 2})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah since we're just transforming invalid code at runtime to more invalid code that seems fine. We could probably include this in a new rule too 😬

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use itertools::Itertools;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_python_ast::{self as ast, Expr, Keyword};
Expand Down Expand Up @@ -53,9 +55,12 @@ impl AlwaysFixableViolation for UnnecessaryDictKwargs {

/// PIE804
pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, expr: &Expr, kwargs: &[Keyword]) {
let mut all_kwargs = HashSet::new();

for kw in kwargs {
// keyword is a spread operator (indicated by None)
if kw.arg.is_some() {
if let Some(name) = &kw.arg {
all_kwargs.insert(name.as_str());
continue;
}

Expand All @@ -81,6 +86,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, expr: &Expr, kwargs
let kwargs = keys
.iter()
.filter_map(|key| key.as_ref().and_then(as_kwarg))
.filter(|name| all_kwargs.insert(name))
.collect::<Vec<_>>();
if kwargs.len() != keys.len() {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/ruff_linter/src/rules/flake8_pie/mod.rs
assertion_line: 29
---
PIE804.py:1:1: PIE804 [*] Unnecessary `dict` kwargs
|
Expand Down Expand Up @@ -119,4 +120,19 @@ PIE804.py:11:1: PIE804 [*] Unnecessary `dict` kwargs
13 13 |
14 14 | foo(**{**data, "foo": "buzz"})

PIE804.py:25:1: PIE804 [*] Unnecessary `dict` kwargs
|
24 | # Duplicated key names wont be fixed to avoid syntax error.
25 | abc(**{'a': b}, **{'a': c}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PIE804
|
= help: Remove unnecessary kwargs

ℹ Fix
22 22 | abc(**{"for": 3})
23 23 |
24 24 | # Duplicated key names wont be fixed to avoid syntax error.
25 |-abc(**{'a': b}, **{'a': c}) # PIE804
25 |+abc(a=b, **{'a': c}) # PIE804


Loading