[flake8-comprehensions] Document RecursionError edge case in __len__ (C416)#25286
Merged
Merged
Conversation
… `__len__` Reaching `[item for item in self]` (and the dict/set equivalents) from inside `__len__` is safe today, but the C416 auto-fix replaces it with `list(self)`, which CPython calls back into `__len__` via the length hint optimization, recursing infinitely. The rule's heuristic intentionally doesn't try to detect this control flow. Document the failure mode in the rule's "Known problems" so users can suppress with `# noqa: C416` rather than discovering it at runtime. Closes astral-sh#13752.
|
flake8-comprehensions] Document C416 RecursionError edge case in __len__flake8-comprehensions] DocumentRecursionError edge case in __len__ (C416)
flake8-comprehensions] DocumentRecursionError edge case in __len__ (C416)flake8-comprehensions] Document RecursionError edge case in __len__ (C416)
thejchap
pushed a commit
to thejchap/ruff
that referenced
this pull request
May 23, 2026
…en__` (`C416`) (astral-sh#25286) 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 <[email protected]>
anishgirianish
pushed a commit
to anishgirianish/ruff
that referenced
this pull request
May 28, 2026
…en__` (`C416`) (astral-sh#25286) 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 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
C416's autofix rewrites a list comprehension like
[item for item in self]intolist(self), but when the original is reached from inside__len__, the rewrite recurses: CPython'slist()constructor probes__length_hint__(which dispatches to__len__) as a sizing optimization while iterating, solist(self)from inside__len__calls__len__again. The same applies todict()andset()calls onself.In #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: C416workaround for when the comprehension is reached (directly or transitively) from__len__.The change is docstring-only.
cargo dev generate-allanduvx prek run --from-ref mainboth pass.fixes #13752