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

[Fix error] C417 and walrus statements #14808

Closed
zeevox opened this issue Dec 6, 2024 · 1 comment · Fixed by #14827
Closed

[Fix error] C417 and walrus statements #14808

zeevox opened this issue Dec 6, 2024 · 1 comment · Fixed by #14827
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome

Comments

@zeevox
Copy link

zeevox commented Dec 6, 2024

Applying fixes for rule C417 introduces a syntax error when the second map argument is a walrus assignment operator.

Minimal example below.
I'm using ruff 0.8.2 and Python 3.12.7 to test this.
For a more complete example, this issue first arose in my solution to Advent of Code 2019 Day 3.

a = [1, 2, 3]
b = map(lambda x: x, c := a)
print(c)

Fix applied by ruff check test.py --select C417 --fix --unsafe-fixes

-b = map(lambda x: x, c := a)
+b = (x for x in c := a)

is syntactically incorrect

  File "/home/zeevox/test.py", line 2
    b = (x for x in c := a)
                      ^^
SyntaxError: invalid syntax

You would think wrapping the walrus statement in parentheses would suffice, like

b = (x for x in (c := a))

And indeed, ruff believes all is well

% ruff check test.py --select C417
All checks passed!

but this too, in fact, raises a syntax error

  File "/home/zeevox/test.py", line 2
    b = (x for x in (c := a))
                     ^^^^^^
SyntaxError: assignment expression cannot be used in a comprehension iterable expression

I wonder whether the real solution is to extract the assignment onto the previous line, or not show the warning in the first place.

@AlexWaygood AlexWaygood added bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome labels Dec 6, 2024
@harupy

This comment has been minimized.

charliermarsh pushed a commit that referenced this issue Dec 7, 2024
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants