Skip to content

PLW3301: incorrect autofix when using generator expression #4410

@oefe

Description

@oefe

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

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions