Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d33f1f

Browse files
Add typing_extensions.get_annotations (#423)
Co-authored-by: Alex Waygood <[email protected]>
1 parent e1250ff commit 2d33f1f

File tree

4 files changed

+660
-0
lines changed

4 files changed

+660
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Release 4.12.2 (June 7, 2024)
22

3+
- Add `typing_extensions.get_annotations`, a backport of
4+
`inspect.get_annotations` that adds features specified
5+
by PEP 649. Patch by Jelle Zijlstra.
36
- Fix regression in v4.12.0 where specialization of certain
47
generics with an overridden `__eq__` method would raise errors.
58
Patch by Jelle Zijlstra.

doc/index.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,25 @@ Functions
747747

748748
.. versionadded:: 4.2.0
749749

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+
750769
.. function:: get_args(tp)
751770

752771
See :py:func:`typing.get_args`. In ``typing`` since 3.8.
@@ -857,6 +876,45 @@ Functions
857876

858877
.. versionadded:: 4.1.0
859878

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
860918

861919
Annotation metadata
862920
~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)