@@ -747,6 +747,25 @@ Functions
747
747
748
748
.. versionadded :: 4.2.0
749
749
750
+ .. function :: get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=Format.VALUE)
751
+
752
+ See :py:func: `inspect.get_annotations `. In the standard library since Python 3.10.
753
+
754
+ ``typing_extensions `` adds the keyword argument ``format ``, as specified
755
+ by :pep: `649 `. The supported formats are listed in the :class: `Format ` enum.
756
+ The default format, :attr: `Format.VALUE `, behaves the same across all versions.
757
+ For the other two formats, ``typing_extensions `` provides a rough approximation
758
+ of the :pep: `649 ` behavior on versions of Python that do not support it.
759
+
760
+ The purpose of this backport is to allow users who would like to use
761
+ :attr: `Format.FORWARDREF ` or :attr: `Format.SOURCE ` semantics once
762
+ :pep: `649 ` is implemented, but who also
763
+ want to support earlier Python versions, to simply write::
764
+
765
+ typing_extensions.get_annotations(obj, format=Format.FORWARDREF)
766
+
767
+ .. versionadded :: 4.13.0
768
+
750
769
.. function :: get_args(tp)
751
770
752
771
See :py:func: `typing.get_args `. In ``typing `` since 3.8.
@@ -857,6 +876,45 @@ Functions
857
876
858
877
.. versionadded :: 4.1.0
859
878
879
+ Enums
880
+ ~~~~~
881
+
882
+ .. class :: Format
883
+
884
+ The formats for evaluating annotations introduced by :pep: `649 `.
885
+ Members of this enum can be passed as the *format * argument
886
+ to :func: `get_annotations `.
887
+
888
+ The final place of this enum in the standard library has not yet
889
+ been determined (see :pep: `649 ` and :pep: `749 `), but the names
890
+ and integer values are stable and will continue to work.
891
+
892
+ .. attribute :: VALUE
893
+
894
+ Equal to 1. The default value. The function will return the conventional Python values
895
+ for the annotations. This format is identical to the return value for
896
+ the function under earlier versions of Python.
897
+
898
+ .. attribute :: FORWARDREF
899
+
900
+ Equal to 2. When :pep: `649 ` is implemented, this format will attempt to return the
901
+ conventional Python values for the annotations. However, if it encounters
902
+ an undefined name, it dynamically creates a proxy object (a ForwardRef)
903
+ that substitutes for that value in the expression.
904
+
905
+ ``typing_extensions `` emulates this value on versions of Python which do
906
+ not support :pep: `649 ` by returning the same value as for ``VALUE `` semantics.
907
+
908
+ .. attribute :: SOURCE
909
+
910
+ Equal to 3. When :pep: `649 ` is implemented, this format will produce an annotation
911
+ dictionary where the values have been replaced by strings containing
912
+ an approximation of the original source code for the annotation expressions.
913
+
914
+ ``typing_extensions `` emulates this by evaluating the annotations using
915
+ ``VALUE `` semantics and then stringifying the results.
916
+
917
+ .. versionadded :: 4.13.0
860
918
861
919
Annotation metadata
862
920
~~~~~~~~~~~~~~~~~~~
0 commit comments