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

PLW3301: incorrect autofix when using generator expression #4410

Closed
oefe opened this issue May 13, 2023 · 2 comments · Fixed by #4412
Closed

PLW3301: incorrect autofix when using generator expression #4410

oefe opened this issue May 13, 2023 · 2 comments · Fixed by #4412
Assignees
Labels
bug Something isn't working

Comments

@oefe
Copy link

oefe commented May 13, 2023

The autofix for PLW3301 generates incorrect code if the nested call is using a generator expression.

Example

PLW3301.py:

x = max(1, max(i for i in range(10)))

Observed

ruff --isolated --diff --select PLW3301  PLW3301.py
--- PLW3301.py
+++ PLW3301.py
@@ -1 +1 @@
-x = max(1, max(i for i in range(10)))
+x = max(1, (i for i in range(10)))

Would fix 1 error.

Issue

This is incorrect. max takes either an iterable, or a variable number of arguments, but not both:

python -c "max(1, (i for i in range(10)))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: '>' not supported between instances of 'generator' and 'int'

Expected

The correct fix would be:

max(1, *(i for i in range(10)))

Ruff Version

ruff --version
ruff 0.0.267
@charliermarsh charliermarsh added the bug Something isn't working label May 13, 2023
@JonathanPlasse
Copy link
Contributor

I can work on it.

@charliermarsh
Copy link
Member

We may just want to avoid including max calls with a single argument altogether, since max seems to assume that the single argument is iterable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants