Skip to content

Commit

Permalink
[flake8-comprehensions] Skip iterables with named expressions in `u…
Browse files Browse the repository at this point in the history
…nnecessary-map` (`C417`) (#14827)

This PR modifies [unnecessary-map
(C417)](https://docs.astral.sh/ruff/rules/unnecessary-map/#unnecessary-map-c417)
to skip `map` expressions if the iterable contains a named expression,
since those cannot appear in comprehensions.

Closes #14808
  • Loading branch information
dylwil3 authored Dec 7, 2024
1 parent 8fdd880 commit 2c13e65
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ def func(arg1: int, arg2: int = 4):
map(lambda x: x, y if y else z)
map(lambda x: x, (y if y else z))
map(lambda x: x, (x, y, z))

# See https://github.com/astral-sh/ruff/issues/14808
# The following should be Ok since
# named expressions are a syntax error inside comprehensions
a = [1, 2, 3]
b = map(lambda x: x, c := a)
print(c)

# Check nested as well
map(lambda x:x, [c:=a])
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use ruff_diagnostics::{Diagnostic, Fix};
use ruff_diagnostics::{FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::helpers::any_over_expr;
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{self as ast, Arguments, Expr, ExprContext, Parameters, Stmt};
Expand Down Expand Up @@ -99,11 +100,17 @@ pub(crate) fn unnecessary_map(
// Only flag, e.g., `map(lambda x: x + 1, iterable)`.
let [Expr::Lambda(ast::ExprLambda {
parameters, body, ..
}), _] = args
}), iterable] = args
else {
return;
};

// For example, (x+1 for x in (c:=a)) is invalid syntax
// so we can't suggest it.
if any_over_expr(iterable, &|expr| expr.is_named_expr()) {
return;
}

if parameters.as_ref().is_some_and(|parameters| {
late_binding(parameters, body)
|| parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ C417.py:47:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr
47 |+(x for x in (y if y else z))
48 48 | map(lambda x: x, (y if y else z))
49 49 | map(lambda x: x, (x, y, z))
50 50 |

C417.py:48:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expression)
|
Expand All @@ -347,13 +348,17 @@ C417.py:48:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr
48 |-map(lambda x: x, (y if y else z))
48 |+(x for x in (y if y else z))
49 49 | map(lambda x: x, (x, y, z))
50 50 |
51 51 | # See https://github.com/astral-sh/ruff/issues/14808

C417.py:49:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expression)
|
47 | map(lambda x: x, y if y else z)
48 | map(lambda x: x, (y if y else z))
49 | map(lambda x: x, (x, y, z))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ C417
50 |
51 | # See https://github.com/astral-sh/ruff/issues/14808
|
= help: Replace `map()` with a generator expression

Expand All @@ -363,3 +368,6 @@ C417.py:49:1: C417 [*] Unnecessary `map()` usage (rewrite using a generator expr
48 48 | map(lambda x: x, (y if y else z))
49 |-map(lambda x: x, (x, y, z))
49 |+(x for x in (x, y, z))
50 50 |
51 51 | # See https://github.com/astral-sh/ruff/issues/14808
52 52 | # The following should be Ok since

0 comments on commit 2c13e65

Please sign in to comment.