ruff
8a8e35eb - [`flake8-comprehensions`] Skip `C417` for lambdas with positional-only parameters (#25272)

Commit
1 day ago
[`flake8-comprehensions`] Skip `C417` for lambdas with positional-only parameters (#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.
Author
Parents
Loading