15
15
"call_evaluate_function" ,
16
16
"get_annotate_function" ,
17
17
"get_annotations" ,
18
- "annotations_to_source " ,
19
- "value_to_source " ,
18
+ "annotations_to_string " ,
19
+ "value_to_string " ,
20
20
]
21
21
22
22
23
23
class Format (enum .IntEnum ):
24
24
VALUE = 1
25
25
FORWARDREF = 2
26
- SOURCE = 3
26
+ STRING = 3
27
27
28
28
29
29
_Union = None
@@ -291,9 +291,21 @@ def __convert_to_ast(self, other):
291
291
return other .__ast_node__
292
292
elif isinstance (other , slice ):
293
293
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
+ ),
297
309
)
298
310
else :
299
311
return ast .Constant (value = other )
@@ -469,7 +481,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
469
481
can be called with any of the format arguments in the Format enum, but
470
482
compiler-generated __annotate__ functions only support the VALUE format.
471
483
This function provides additional functionality to call __annotate__
472
- functions with the FORWARDREF and SOURCE formats.
484
+ functions with the FORWARDREF and STRING formats.
473
485
474
486
*annotate* must be an __annotate__ function, which takes a single argument
475
487
and returns a dict of annotations.
@@ -487,8 +499,8 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
487
499
return annotate (format )
488
500
except NotImplementedError :
489
501
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
492
504
# environment where every name lookup results in an instance of _Stringifier.
493
505
# _Stringifier supports every dunder operation and returns a new _Stringifier.
494
506
# 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):
524
536
for key , val in annos .items ()
525
537
}
526
538
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,
528
540
# 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
530
542
# name lookups result in _Stringifier objects, FORWARDREF uses the globals
531
543
# and builtins, so that defined names map to their real values.
532
544
# Second, instead of returning strings, we want to return either real values
@@ -688,14 +700,14 @@ def get_annotations(
688
700
# __annotations__ threw NameError and there is no __annotate__. In that case,
689
701
# we fall back to trying __annotations__ again.
690
702
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__
693
705
ann = _get_and_call_annotate (obj , format )
694
706
if ann is not None :
695
707
return ann
696
708
# But if we didn't get it, we use __annotations__ instead.
697
709
ann = _get_dunder_annotations (obj )
698
- return annotations_to_source (ann )
710
+ return annotations_to_string (ann )
699
711
case _:
700
712
raise ValueError (f"Unsupported format { format !r} " )
701
713
@@ -764,10 +776,10 @@ def get_annotations(
764
776
return return_value
765
777
766
778
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.
769
781
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
771
783
not have access to the code that originally produced the annotations. It uses
772
784
repr() for most objects.
773
785
@@ -783,10 +795,10 @@ def value_to_source(value):
783
795
return repr (value )
784
796
785
797
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."""
788
800
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 )
790
802
for n , t in annotations .items ()
791
803
}
792
804
0 commit comments