Repository navigation
Conversation
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.
I am opening this PR to keep nested comprehensions inside the value yielded by a generator expression. The code below currently decompiles to invalid source:
The current output flattens the inner comprehension into the outer generator's clauses:
The dedicated generator simulator loses which collection is constructed for each yielded row. The new
generator_parts_from_bodyhelper instead takes the statement parser's For/If/yield tree and derives the outer generator's clauses while leaving the yielded nested-comprehension expression intact. It restores the actual outer iterable at the call site.Variations include inner list and set comprehensions, filters, nested comprehensions used as call arguments, and unpacked dictionary targets:
The extractor accepts only a completely recovered generator-expression-shaped tree. Unsupported assignments, loop-else suites and multiple sibling yield paths make extraction fail; they cannot disappear into an apparently valid expression. Structural pass, None-return and constant-expression remnants are tolerated. Other shapes retain the existing fallback.
The new route applies to Python 3.12+ generators containing
LOAD_FAST_AND_CLEAR, matching the introduction of PEP 709's inlined comprehensions. The source constructs are older: nested list-comprehension generators begin in Python 2.4; dictionary/set comprehensions are available in Python 3.0 and the Python 2.7 backport.docs/LIMITATIONS.mdremoves the repaired minimal nested-comprehension limitation while retaining the full-module verification backlog for the more complex package examples.