[mypyc] Fix ClassVar self-references in class bodies#21011
Open
VaggelisD wants to merge 2 commits intopython:masterfrom
Open
[mypyc] Fix ClassVar self-references in class bodies#21011VaggelisD wants to merge 2 commits intopython:masterfrom
ClassVar self-references in class bodies#21011VaggelisD wants to merge 2 commits intopython:masterfrom
Conversation
In CPython, the class body executes as a function where earlier
assignments are available to later ones (e.g. C = A | B where A
is a ClassVar defined earlier in the same class). mypyc previously
resolved such names via load_global(), looking them up in the module
globals dict where they don't exist — causing a KeyError at runtime.
Fix by tracking ClassVar names as they're defined during class body
processing, and redirecting lookups to the class being built: the
type object (py_get_attr) for extension classes, or the class dict
(dict_get_item_op) for non-extension classes.
This enables patterns like:
class Parser:
TYPE_TOKENS: ClassVar = {"INT", "VARCHAR"}
FUNC_TOKENS: ClassVar = TYPE_TOKENS | {"FUNCTION"}
for more information, see https://pre-commit.ci
Member
|
IIUC this fixes mypyc/mypyc#972. If yes, could you please updated PR description to start with "Fixes mypyc/mypyc#972"? |
Member
|
Btw thanks for all the PRs! Please note that we will have a release soon, so if you want these PRs included in v1.20, please ping @JukkaL (who will be release manager). |
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.
In CPython, the class body executes as a function where earlier assignments are available to later ones e.g this is possible:
mypyc previously resolved such names via
load_global(), looking them up in the module globals dict where they don't exist causing a KeyError at runtime.This PR fixes this by tracking
ClassVarnames as they're defined during class body processing, and redirecting lookups to the class being built: the type object (py_get_attr) for extension classes, or the class dict (dict_get_item_op) for non-extension classes.