@@ -305,7 +305,13 @@ def __convert_to_ast(self, other):
305305 if isinstance (other .__ast_node__ , str ):
306306 return ast .Name (id = other .__ast_node__ ), other .__extra_names__
307307 return other .__ast_node__ , other .__extra_names__
308- elif other is None or type (other ) in (str , int , float , bool , complex ):
308+ elif (
309+ # In STRING format we don't bother with the create_unique_name() dance;
310+ # it's better to emit the repr() of the object instead of an opaque name.
311+ self .__stringifier_dict__ .format == Format .STRING
312+ or other is None
313+ or type (other ) in (str , int , float , bool , complex )
314+ ):
309315 return ast .Constant (value = other ), None
310316 elif type (other ) is dict :
311317 extra_names = {}
@@ -519,14 +525,15 @@ def unary_op(self):
519525
520526
521527class _StringifierDict (dict ):
522- def __init__ (self , namespace , globals = None , owner = None , is_class = False ):
528+ def __init__ (self , namespace , * , globals = None , owner = None , is_class = False , format ):
523529 super ().__init__ (namespace )
524530 self .namespace = namespace
525531 self .globals = globals
526532 self .owner = owner
527533 self .is_class = is_class
528534 self .stringifiers = []
529535 self .next_id = 1
536+ self .format = format
530537
531538 def __missing__ (self , key ):
532539 fwdref = _Stringifier (
@@ -587,7 +594,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
587594 # possibly constants if the annotate function uses them directly). We then
588595 # convert each of those into a string to get an approximation of the
589596 # original source.
590- globals = _StringifierDict ({})
597+ globals = _StringifierDict ({}, format = format )
591598 if annotate .__closure__ :
592599 freevars = annotate .__code__ .co_freevars
593600 new_closure = []
@@ -637,9 +644,10 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
637644 is_class = isinstance (owner , type )
638645 globals = _StringifierDict (
639646 namespace ,
640- annotate .__globals__ ,
641- owner ,
642- is_class ,
647+ globals = annotate .__globals__ ,
648+ owner = owner ,
649+ is_class = is_class ,
650+ format = format ,
643651 )
644652 if annotate .__closure__ :
645653 freevars = annotate .__code__ .co_freevars
0 commit comments