Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Don't load forward ref targets while setting up non-ext __annotations__ #14401

Commits on Jan 6, 2023

  1. [mypyc] Don't load forward ref targets in non-ext class __annotations__

    Take this example:
    
        from typing import NamedTuple
    
        class VTableMethod(NamedTuple):
            cls: "ClassIR"
    
        class ClassIR: pass
    
    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.
    ichard26 committed Jan 6, 2023
    Configuration menu
    Copy the full SHA
    b867e24 View commit details
    Browse the repository at this point in the history