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

[flake8-comprehensions] Skip when TypeError present from too many (kw)args for C410,C411, and C418 #15838

Merged
merged 3 commits into from
Jan 30, 2025
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 @@ -11,3 +11,7 @@
list([ # comment
1, 2
])

# Skip when too many positional arguments
# See https://github.com/astral-sh/ruff/issues/15810
list([1],[2])
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
x = [1, 2, 3]
list([i for i in x])

# Skip when too many positional arguments
# or keyword argument present.
# See https://github.com/astral-sh/ruff/issues/15810
list([x for x in "XYZ"],[])
list([x for x in "XYZ"],foo=[])
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
dict({}, a=1)
dict({x: 1 for x in range(1)}, a=1)

# Skip when too many positional arguments
# See https://github.com/astral-sh/ruff/issues/15810
dict({"A": 1}, {"B": 2})
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
flake8_comprehensions::rules::unnecessary_literal_within_dict_call(checker, call);
}
if checker.enabled(Rule::UnnecessaryListCall) {
flake8_comprehensions::rules::unnecessary_list_call(checker, expr, func, args);
flake8_comprehensions::rules::unnecessary_list_call(checker, expr, call);
}
if checker.enabled(Rule::UnnecessaryCallAroundSorted) {
flake8_comprehensions::rules::unnecessary_call_around_sorted(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::Expr;
use ruff_python_ast::{Arguments, Expr, ExprCall};

use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
Expand Down Expand Up @@ -44,12 +44,27 @@ impl AlwaysFixableViolation for UnnecessaryListCall {
}

/// C411
pub(crate) fn unnecessary_list_call(
checker: &mut Checker,
expr: &Expr,
func: &Expr,
args: &[Expr],
) {
pub(crate) fn unnecessary_list_call(checker: &mut Checker, expr: &Expr, call: &ExprCall) {
let ExprCall {
func,
arguments,
range: _,
} = call;

if !arguments.keywords.is_empty() {
return;
}

if arguments.args.len() > 1 {
return;
}

let Arguments {
range: _,
args,
keywords: _,
} = arguments;

let Some(argument) = helpers::first_argument_with_matching_function("list", func, args) else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub(crate) fn unnecessary_literal_within_dict_call(checker: &mut Checker, call:
if !call.arguments.keywords.is_empty() {
return;
}
if call.arguments.args.len() > 1 {
return;
}
let Some(argument) =
helpers::first_argument_with_matching_function("dict", &call.func, &call.arguments.args)
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub(crate) fn unnecessary_literal_within_list_call(checker: &mut Checker, call:
if !call.arguments.keywords.is_empty() {
return;
}
if call.arguments.args.len() > 1 {
return;
}
let Some(argument) =
helpers::first_argument_with_matching_function("list", &call.func, &call.arguments.args)
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ C410.py:11:1: C410 [*] Unnecessary list literal passed to `list()` (remove the o
12 | | 1, 2
13 | | ])
| |__^ C410
14 |
15 | # Skip when too many positional arguments
|
= help: Remove outer `list()` call

Expand All @@ -116,3 +118,6 @@ C410.py:11:1: C410 [*] Unnecessary list literal passed to `list()` (remove the o
12 12 | 1, 2
13 |-])
13 |+]
14 14 |
15 15 | # Skip when too many positional arguments
16 16 | # See https://github.com/astral-sh/ruff/issues/15810
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ C411.py:2:1: C411 [*] Unnecessary `list()` call (remove the outer call to `list(
1 | x = [1, 2, 3]
2 | list([i for i in x])
| ^^^^^^^^^^^^^^^^^^^^ C411
3 |
4 | # Skip when too many positional arguments
|
= help: Remove outer `list()` call

ℹ Unsafe fix
1 1 | x = [1, 2, 3]
2 |-list([i for i in x])
2 |+[i for i in x]
3 3 |
4 4 | # Skip when too many positional arguments
5 5 | # or keyword argument present.
Loading