Skip to content

Commit

Permalink
[flake8-comprehensions] Skip when TypeError present from too many…
Browse files Browse the repository at this point in the history
… (kw)args for `C410`,`C411`, and `C418` (#15838)

Both `list` and `dict` expect only a single positional argument. Giving
more positional arguments, or a keyword argument, is a `TypeError` and
neither the lint rule nor its fix make sense in that context.

Closes #15810
  • Loading branch information
dylwil3 authored Jan 30, 2025
1 parent fe516e2 commit 071862a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
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.

0 comments on commit 071862a

Please sign in to comment.