1515 "call_evaluate_function" ,
1616 "get_annotate_function" ,
1717 "get_annotations" ,
18- "annotations_to_source " ,
19- "value_to_source " ,
18+ "annotations_to_string " ,
19+ "value_to_string " ,
2020]
2121
2222
2323class Format (enum .IntEnum ):
2424 VALUE = 1
2525 FORWARDREF = 2
26- SOURCE = 3
26+ STRING = 3
2727
2828
2929_Union = None
@@ -291,9 +291,21 @@ def __convert_to_ast(self, other):
291291 return other .__ast_node__
292292 elif isinstance (other , slice ):
293293 return ast .Slice (
294- lower = self .__convert_to_ast (other .start ) if other .start is not None else None ,
295- upper = self .__convert_to_ast (other .stop ) if other .stop is not None else None ,
296- step = self .__convert_to_ast (other .step ) if other .step is not None else None ,
294+ lower = (
295+ self .__convert_to_ast (other .start )
296+ if other .start is not None
297+ else None
298+ ),
299+ upper = (
300+ self .__convert_to_ast (other .stop )
301+ if other .stop is not None
302+ else None
303+ ),
304+ step = (
305+ self .__convert_to_ast (other .step )
306+ if other .step is not None
307+ else None
308+ ),
297309 )
298310 else :
299311 return ast .Constant (value = other )
@@ -469,7 +481,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
469481 can be called with any of the format arguments in the Format enum, but
470482 compiler-generated __annotate__ functions only support the VALUE format.
471483 This function provides additional functionality to call __annotate__
472- functions with the FORWARDREF and SOURCE formats.
484+ functions with the FORWARDREF and STRING formats.
473485
474486 *annotate* must be an __annotate__ function, which takes a single argument
475487 and returns a dict of annotations.
@@ -487,8 +499,8 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
487499 return annotate (format )
488500 except NotImplementedError :
489501 pass
490- if format == Format .SOURCE :
491- # SOURCE is implemented by calling the annotate function in a special
502+ if format == Format .STRING :
503+ # STRING is implemented by calling the annotate function in a special
492504 # environment where every name lookup results in an instance of _Stringifier.
493505 # _Stringifier supports every dunder operation and returns a new _Stringifier.
494506 # At the end, we get a dictionary that mostly contains _Stringifier objects (or
@@ -524,9 +536,9 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
524536 for key , val in annos .items ()
525537 }
526538 elif format == Format .FORWARDREF :
527- # FORWARDREF is implemented similarly to SOURCE , but there are two changes,
539+ # FORWARDREF is implemented similarly to STRING , but there are two changes,
528540 # at the beginning and the end of the process.
529- # First, while SOURCE uses an empty dictionary as the namespace, so that all
541+ # First, while STRING uses an empty dictionary as the namespace, so that all
530542 # name lookups result in _Stringifier objects, FORWARDREF uses the globals
531543 # and builtins, so that defined names map to their real values.
532544 # Second, instead of returning strings, we want to return either real values
@@ -688,14 +700,14 @@ def get_annotations(
688700 # __annotations__ threw NameError and there is no __annotate__. In that case,
689701 # we fall back to trying __annotations__ again.
690702 return dict (_get_dunder_annotations (obj ))
691- case Format .SOURCE :
692- # For SOURCE , we try to call __annotate__
703+ case Format .STRING :
704+ # For STRING , we try to call __annotate__
693705 ann = _get_and_call_annotate (obj , format )
694706 if ann is not None :
695707 return ann
696708 # But if we didn't get it, we use __annotations__ instead.
697709 ann = _get_dunder_annotations (obj )
698- return annotations_to_source (ann )
710+ return annotations_to_string (ann )
699711 case _:
700712 raise ValueError (f"Unsupported format { format !r} " )
701713
@@ -764,10 +776,10 @@ def get_annotations(
764776 return return_value
765777
766778
767- def value_to_source (value ):
768- """Convert a Python value to a format suitable for use with the SOURCE format.
779+ def value_to_string (value ):
780+ """Convert a Python value to a format suitable for use with the STRING format.
769781
770- This is inteded as a helper for tools that support the SOURCE format but do
782+ This is inteded as a helper for tools that support the STRING format but do
771783 not have access to the code that originally produced the annotations. It uses
772784 repr() for most objects.
773785
@@ -783,10 +795,10 @@ def value_to_source(value):
783795 return repr (value )
784796
785797
786- def annotations_to_source (annotations ):
787- """Convert an annotation dict containing values to approximately the SOURCE format."""
798+ def annotations_to_string (annotations ):
799+ """Convert an annotation dict containing values to approximately the STRING format."""
788800 return {
789- n : t if isinstance (t , str ) else value_to_source (t )
801+ n : t if isinstance (t , str ) else value_to_string (t )
790802 for n , t in annotations .items ()
791803 }
792804
0 commit comments