{{ message }}
gh-150700: Fix class-scope inline comprehensions when nested scopes reference __class__ and friends - #150735
Merged
carljm merged 7 commits intoJun 9, 2026
Conversation
…_class__` and friends In `inline_comprehension()`, when `__class__` / `__classdict__` / `__conditional_annotations__` appears as `FREE` in a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded from `comp_free` unconditionally. This prevents `drop_class_free()` from seeing it, so the appropriate `ste_needs_(...)` flag is never set on the enclosing class. That leads to `codegen_make_closure()` throwing `SystemError` when it couldn't find `__class__` / `__classdict__` / `__conditional_annotations__` in the class's cellvars. From now on we just discard from `comp_free` when no child scope (e.g. a lambda) still needs the name as `FREE`. When a child scope does need it, keep it in `comp_free` so `drop_class_free()` can set the appropriate flag and the class creates the implicit cell.
…th-lambda-raises-syste
Member
Author
johnslavik
commented
Jun 1, 2026
carljm
approved these changes
Jun 3, 2026
carljm
left a comment
Member
There was a problem hiding this comment.
This looks good to me. Might be nice to have some tests that validate more than "compilation doesn't fail" but actually check we are getting the right value out of __class__.
Member
Author
Excellent, added. |
carljm
approved these changes
Jun 9, 2026
Member
|
Thanks for the fix! |
|
Thanks @johnslavik for the PR, and @carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-151211 is a backport of this pull request to the 3.15 branch. |
|
GH-151212 is a backport of this pull request to the 3.14 branch. |
carljm
pushed a commit
that referenced
this pull request
Jun 9, 2026
…copes reference `__class__` and friends (GH-150735) (#151212) gh-150700: Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends (GH-150735) * Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends In `inline_comprehension()`, when `__class__` / `__classdict__` / `__conditional_annotations__` appears as `FREE` in a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded from `comp_free` unconditionally. This prevents `drop_class_free()` from seeing it, so the appropriate `ste_needs_(...)` flag is never set on the enclosing class. That leads to `codegen_make_closure()` throwing `SystemError` when it couldn't find `__class__` / `__classdict__` / `__conditional_annotations__` in the class's cellvars. From now on we just discard from `comp_free` when no child scope (e.g. a lambda) still needs the name as `FREE`. When a child scope does need it, keep it in `comp_free` so `drop_class_free()` can set the appropriate flag and the class creates the implicit cell. * Fix tests * Fix typo * Fix formatting * Add test checking validity of `__class__` returned * Prefer 'used' to 'deferred' (cherry picked from commit ce916dc) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
carljm
pushed a commit
that referenced
this pull request
Jun 9, 2026
…copes reference `__class__` and friends (GH-150735) (#151211) gh-150700: Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends (GH-150735) * Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends In `inline_comprehension()`, when `__class__` / `__classdict__` / `__conditional_annotations__` appears as `FREE` in a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded from `comp_free` unconditionally. This prevents `drop_class_free()` from seeing it, so the appropriate `ste_needs_(...)` flag is never set on the enclosing class. That leads to `codegen_make_closure()` throwing `SystemError` when it couldn't find `__class__` / `__classdict__` / `__conditional_annotations__` in the class's cellvars. From now on we just discard from `comp_free` when no child scope (e.g. a lambda) still needs the name as `FREE`. When a child scope does need it, keep it in `comp_free` so `drop_class_free()` can set the appropriate flag and the class creates the implicit cell. * Fix tests * Fix typo * Fix formatting * Add test checking validity of `__class__` returned * Prefer 'used' to 'deferred' (cherry picked from commit ce916dc) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
philthompson10
pushed a commit
to philthompson10/cpython
that referenced
this pull request
Jun 17, 2026
…opes reference `__class__` and friends (python#150735) * Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends In `inline_comprehension()`, when `__class__` / `__classdict__` / `__conditional_annotations__` appears as `FREE` in a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded from `comp_free` unconditionally. This prevents `drop_class_free()` from seeing it, so the appropriate `ste_needs_(...)` flag is never set on the enclosing class. That leads to `codegen_make_closure()` throwing `SystemError` when it couldn't find `__class__` / `__classdict__` / `__conditional_annotations__` in the class's cellvars. From now on we just discard from `comp_free` when no child scope (e.g. a lambda) still needs the name as `FREE`. When a child scope does need it, keep it in `comp_free` so `drop_class_free()` can set the appropriate flag and the class creates the implicit cell. * Fix tests * Fix typo * Fix formatting * Add test checking validity of `__class__` returned * Prefer 'used' to 'deferred'
johnslavik
deleted the
gh-150700-class-scope-comprehension-with-lambda-raises-syste
branch
July 21, 2026 01:55
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.

Follow up to GH-120295.
In
inline_comprehension(), when__class__/__classdict__/__conditional_annotations__appears asFREEin a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded fromcomp_freeunconditionally. This preventsdrop_class_free()from seeing it, so the appropriateste_needs_(...)flag is never set on the enclosing class. That leads tocodegen_make_closure()throwingSystemErrorwhen it couldn't find__class__/__classdict__/__conditional_annotations__in the class's cellvars.From now on we just discard from
comp_freewhen no child scope (e.g. a lambda) still needs the name asFREE. When a child scope does need it, keep it incomp_freesodrop_class_free()can set the appropriate flag and the class creates the implicit cell.