@@ -45,6 +45,7 @@ class Format(enum.IntEnum):
4545 "__globals__" ,
4646 "__owner__" ,
4747 "__cell__" ,
48+ "__stringifier_dict__" ,
4849)
4950
5051
@@ -268,7 +269,16 @@ class _Stringifier:
268269 # instance of the other in place.
269270 __slots__ = _SLOTS
270271
271- def __init__ (self , node , globals = None , owner = None , is_class = False , cell = None ):
272+ def __init__ (
273+ self ,
274+ node ,
275+ globals = None ,
276+ owner = None ,
277+ is_class = False ,
278+ cell = None ,
279+ * ,
280+ stringifier_dict ,
281+ ):
272282 # Either an AST node or a simple str (for the common case where a ForwardRef
273283 # represent a single name).
274284 assert isinstance (node , (ast .AST , str ))
@@ -283,6 +293,7 @@ def __init__(self, node, globals=None, owner=None, is_class=False, cell=None):
283293 self .__globals__ = globals
284294 self .__cell__ = cell
285295 self .__owner__ = owner
296+ self .__stringifier_dict__ = stringifier_dict
286297
287298 def __convert_to_ast (self , other ):
288299 if isinstance (other , _Stringifier ):
@@ -317,9 +328,15 @@ def __get_ast(self):
317328 return node
318329
319330 def __make_new (self , node ):
320- return _Stringifier (
321- node , self .__globals__ , self .__owner__ , self .__forward_is_class__
331+ stringifier = _Stringifier (
332+ node ,
333+ self .__globals__ ,
334+ self .__owner__ ,
335+ self .__forward_is_class__ ,
336+ stringifier_dict = self .__stringifier_dict__ ,
322337 )
338+ self .__stringifier_dict__ .stringifiers .append (stringifier )
339+ return stringifier
323340
324341 # Must implement this since we set __eq__. We hash by identity so that
325342 # stringifiers in dict keys are kept separate.
@@ -462,6 +479,7 @@ def __missing__(self, key):
462479 globals = self .globals ,
463480 owner = self .owner ,
464481 is_class = self .is_class ,
482+ stringifier_dict = self ,
465483 )
466484 self .stringifiers .append (fwdref )
467485 return fwdref
@@ -516,7 +534,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
516534 name = freevars [i ]
517535 else :
518536 name = "__cell__"
519- fwdref = _Stringifier (name )
537+ fwdref = _Stringifier (name , stringifier_dict = globals )
520538 new_closure .append (types .CellType (fwdref ))
521539 closure = tuple (new_closure )
522540 else :
@@ -573,6 +591,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
573591 owner = owner ,
574592 globals = annotate .__globals__ ,
575593 is_class = is_class ,
594+ stringifier_dict = globals ,
576595 )
577596 globals .stringifiers .append (fwdref )
578597 new_closure .append (types .CellType (fwdref ))
@@ -591,6 +610,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
591610 result = func (Format .VALUE )
592611 for obj in globals .stringifiers :
593612 obj .__class__ = ForwardRef
613+ obj .__stringifier_dict__ = None # not needed for ForwardRef
594614 if isinstance (obj .__ast_node__ , str ):
595615 obj .__arg__ = obj .__ast_node__
596616 obj .__ast_node__ = None
0 commit comments