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 iterables with named expressions in unnecessary-map (C417) #14827

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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)
dylwil3 marked this conversation as resolved.
Show resolved Hide resolved
print(c)

# Check nested as well
[x for x in 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
Loading