[mypyc] Don't load forward ref targets while setting up non-ext __annotations__ #14401
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.
Take this example:
In irbuild::classdef::add_non_ext_class_attr_ann(), mypyc tries to assign the ClassIR type object to VTableMethod's __annotations__. This causes a segfault as ClassIR won't be initialized and allocated until after the NamedTuple is set up.
Fortunately, AssignmentStmt preserves the unanalyzed type (UnboundType). If
stmt.unanalyzed_type.orginal_str_expr
is not None, then we know we're dealing with a forward ref and should just load the string instead.Unfortunately, it seems difficult (or impossible?) to infer whether an annotation is a forward reference when the annotations future is enabled and the annotation isn't a string.
I guess this fixes mypyc/mypyc#938?
Dataclasses are still broken as strings in __annotations__ cause dataclass's internals to lookup the module in sys.modules which fails (as the module hasn't been fully initialized and added to sys.modules yet!)