Commit 076ad6e
C416's autofix rewrites a list comprehension like `[item for item in
self]` into `list(self)`, but when the original is reached from inside
`__len__`, the rewrite recurses: CPython's `list()` constructor probes
`__length_hint__` (which dispatches to `__len__`) as a sizing
optimization while iterating, so `list(self)` from inside `__len__`
calls `__len__` again. The same applies to `dict()` and `set()` calls on
`self`.
In astral-sh#13752, MichaReiser noted that extending the docs would be reasonable
and that detecting this control flow isn't the goal. I added a paragraph
to the C416 docstring with a runnable example of the failure mode, plus
the `# noqa: C416` workaround for when the comprehension is reached
(directly or transitively) from `__len__`.
The change is docstring-only. `cargo dev generate-all` and `uvx prek run
--from-ref main` both pass.
fixes astral-sh#13752
---------
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
1 parent 5c84391 commit 076ad6e
1 file changed
Lines changed: 5 additions & 3 deletions
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
56 | | - | |
| 55 | + | |
| 56 | + | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
0 commit comments