[flake8-comprehensions] Skip C417 for lambdas with positional-only parameters#25272
Conversation
| // Reject lambdas that also declare positional-only parameters, e.g. `lambda x, /, y: ...`. | ||
| // Such a lambda requires two arguments; `map()` over a single iterable is already broken | ||
| // at runtime and must not be rewritten as a comprehension. | ||
| if !parameters.posonlyargs.is_empty() { |
There was a problem hiding this comment.
I think we could just check parameters.len() == 1 and also remove the check on line 226.
|
|
Thanks for the suggestion! You're right, checking parameters.len() == 1 is much cleaner and handles all multi-parameter cases uniformly without needing the separate posonlyargs guard. Updated accordingly. |
| let Some(parameters) = lambda.parameters.as_deref() else { | ||
| return false; | ||
| }; | ||
|
|
There was a problem hiding this comment.
I don't think we need to remove these blank lines, and we should be able to remove the check on line 220, as I said before. It's redundant with the total parameters.len() check, unless I'm missing something.
|
Thanks for catching that! Restored the blank lines and removed the vararg/kwarg check since parameters.len() == 1 already makes it redundant.You said: commit message |
flake8-comprehensions] Skip C417 for lambdas with positional-only parameters
|
Thanks for the merge and for the helpful review, ntBre — I really appreciate the suggestion to simplify the arity check. It made the fix cleaner and more robust. |
…y parameters (astral-sh#25272) ## Summary `lambda_has_expected_arity` in the C417 (`UnnecessaryMap`) rule checked `parameters.args` for exactly one element to confirm the lambda takes a single argument, but never checked `parameters.posonlyargs`. A lambda like `lambda x, /, y: x + y` has `posonlyargs = [x]` and `args = [y]`, so the slice pattern matched `y` as the sole element and the function incorrectly returned `true`, causing ruff to raise a false C417 diagnostic and offer a broken autofix for a two-parameter lambda. The fix adds an early return when `parameters.posonlyargs` is non-empty, ensuring lambdas that combine positional-only and regular parameters are correctly rejected before the arity check passes. ## Test Plan Added a new `# Ok` case to `C417.py`: ```python # Ok: lambda has a positional-only parameter alongside a regular parameter; # total arity is 2, so rewriting to a comprehension is incorrect. map(lambda x, /, y: x + y, nums) ``` This line produces no diagnostic after the fix, confirming the false positive is gone.
…y parameters (astral-sh#25272) ## Summary `lambda_has_expected_arity` in the C417 (`UnnecessaryMap`) rule checked `parameters.args` for exactly one element to confirm the lambda takes a single argument, but never checked `parameters.posonlyargs`. A lambda like `lambda x, /, y: x + y` has `posonlyargs = [x]` and `args = [y]`, so the slice pattern matched `y` as the sole element and the function incorrectly returned `true`, causing ruff to raise a false C417 diagnostic and offer a broken autofix for a two-parameter lambda. The fix adds an early return when `parameters.posonlyargs` is non-empty, ensuring lambdas that combine positional-only and regular parameters are correctly rejected before the arity check passes. ## Test Plan Added a new `# Ok` case to `C417.py`: ```python # Ok: lambda has a positional-only parameter alongside a regular parameter; # total arity is 2, so rewriting to a comprehension is incorrect. map(lambda x, /, y: x + y, nums) ``` This line produces no diagnostic after the fix, confirming the false positive is gone.
Summary
lambda_has_expected_arityin the C417 (UnnecessaryMap) rule checkedparameters.argsfor exactly one element to confirm the lambda takes asingle argument, but never checked
parameters.posonlyargs. A lambdalike
lambda x, /, y: x + yhasposonlyargs = [x]andargs = [y],so the slice pattern matched
yas the sole element and the functionincorrectly returned
true, causing ruff to raise a false C417diagnostic and offer a broken autofix for a two-parameter lambda.
The fix adds an early return when
parameters.posonlyargsis non-empty,ensuring lambdas that combine positional-only and regular parameters are
correctly rejected before the arity check passes.
Test Plan
Added a new
# Okcase toC417.py:This line produces no diagnostic after the fix, confirming the false
positive is gone.