[mypyc] Fix ClassVar self-references in class bodies#21011
[mypyc] Fix ClassVar self-references in class bodies#21011VaggelisD wants to merge 2 commits intopython:masterfrom
ClassVar self-references in class bodies#21011Conversation
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
|
IIUC this fixes mypyc/mypyc#972. If yes, could you please updated PR description to start with "Fixes mypyc/mypyc#972"? |
|
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). |
|
Hey @ilevkivskyi, we actually ran into this in independently in SQLGlot, thus the dict-based repro instead of the much simpler
Thank you for the project! We were able to make SQLGlot faster by 4-5x on avg and we only just got started. Hopefully we will be contributing even more fixes and features. Regarding pinging, is asking for a review in Github the preferred way? I'm sure the core contributors are oversubscribed so I've avoided pinging in general. I had also joined the Gitter to discuss these solutions but I'm not sure if it's active anymore? |
Yeah, you can request review or
Yeah, Gitter is not active. We want to create a new Discord server at some point soon, but didn't get to this yet. |
Stating the obvious here but a Discord would be awesome; I can vouch that having to manage a community channel is not easy but it'd be of great help for external contributors, especially for more complex PRs in which upfront coordination & design can massively reduce PR friction later down the road. We'll jump in when it's ready! |
Fixes mypyc/mypyc#972.
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.