Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backport of evaluate_forward_ref #497

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8386b24
first draft
Daraan Oct 22, 2024
21cd9da
1st working draft
Daraan Oct 29, 2024
ade80c9
add tests
Daraan Oct 30, 2024
4521d21
Complete tests and code
Daraan Oct 30, 2024
f65e885
Merge remote-tracking branch 'upstream/main' into 3.14/evaluate_forwa…
Daraan Oct 30, 2024
f4cf6b8
Add missing failure cases
Daraan Oct 30, 2024
aebc55c
more compact code
Daraan Oct 30, 2024
d594cbc
complete Final, Generic cases
Daraan Oct 30, 2024
fb992e8
Solve global variable error
Daraan Oct 30, 2024
a195879
Better test cases for __type_params__ & owner
Daraan Oct 30, 2024
a54d7a9
Added failing test case for 3.10
Daraan Oct 30, 2024
46d3efa
add lint exception
Daraan Oct 30, 2024
2261fd1
Test was backported
Daraan Oct 30, 2024
881f926
Update changelog
Daraan Oct 30, 2024
73fb856
Add doc entry
Daraan Oct 30, 2024
01349f3
removed unnecessary import
Daraan Oct 30, 2024
c6a32ce
Merge branch 'main' into 3.14/evaluate_forward_ref
Daraan Nov 25, 2024
3e68adc
use unittest assert
Daraan Nov 26, 2024
b55d419
Use explicit namespaces
Daraan Nov 26, 2024
7380492
Changed Format.SOURCE to STRING
Daraan Nov 26, 2024
cb45bfd
Update comments
Daraan Nov 26, 2024
2bad4c1
Implement 3.11 like _type_check
Daraan Nov 26, 2024
e7b3014
Merge remote-tracking branch 'refs/remotes/origin/3.14/evaluate_forwa…
Daraan Nov 26, 2024
2615d75
Merge remote-tracking branch 'upstream/main' into 3.14/evaluate_forwa…
Daraan Nov 26, 2024
b054c81
Add lint exception
Daraan Nov 26, 2024
0f400b8
support module keyword for some python versions
Daraan Nov 26, 2024
8b44550
Reorder test
Daraan Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ aliases that have a `Concatenate` special form as their argument.
- Backport CPython PR [#124795](https://github.com/python/cpython/pull/124795):
fix `TypeAliasType` not raising an error on non-tuple inputs for `type_params`.
Patch by [Daraan](https://github.com/Daraan).
- Backport `evaluate_forward_ref` from CPython PR
[#119891](https://github.com/python/cpython/pull/119891) to evaluate `ForwardRef`s.
Patch by [Daraan](https://github.com/Daraan), backporting a CPython PR by Jelle Zijlstra.
- Fix that lists and ... could not be used for parameter expressions for `TypeAliasType`
instances before Python 3.11.
Patch by [Daraan](https://github.com/Daraan).
Expand Down
35 changes: 33 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,37 @@ Functions

.. versionadded:: 4.2.0

.. function:: evaluate_forward_ref(forward_ref, *, owner=None, globals=None, locals=None, type_params=None, format=Format.VALUE)

Evaluate an :py:class:`typing.ForwardRef` as a :py:term:`type hint`.

This is similar to calling :py:meth:`annotationlib.ForwardRef.evaluate`,
but unlike that method, :func:`!evaluate_forward_ref` also:

* Recursively evaluates forward references nested within the type hint.
However, the amount of recursion is limited in Python 3.8 and 3.10.
* Raises :exc:`TypeError` when it encounters certain objects that are
not valid type hints.
* Replaces type hints that evaluate to :const:`!None` with
:class:`types.NoneType`.
* Supports the :attr:`Format.FORWARDREF` and
:attr:`Format.STRING` formats.

*forward_ref* must be an instance of :py:class:`typing.ForwardRef`.
*owner*, if given, should be the object that holds the annotations that
the forward reference derived from, such as a module, class object, or function.
It is used to infer the namespaces to use for looking up names.
*globals* and *locals* can also be explicitly given to provide
the global and local namespaces.
*type_params* is a tuple of :py:ref:`type parameters <type-params>` that
are in scope when evaluating the forward reference.
This parameter must be provided (though it may be an empty tuple) if *owner*
is not given and the forward reference does not already have an owner set.
*format* specifies the format of the annotation and is a member of
the :class:`Format` enum.

.. versionadded:: 4.13.0

.. function:: get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=Format.VALUE)

See :py:func:`inspect.get_annotations`. In the standard library since Python 3.10.
Expand All @@ -764,7 +795,7 @@ Functions
of the :pep:`649` behavior on versions of Python that do not support it.

The purpose of this backport is to allow users who would like to use
:attr:`Format.FORWARDREF` or :attr:`Format.SOURCE` semantics once
:attr:`Format.FORWARDREF` or :attr:`Format.STRING` semantics once
:pep:`649` is implemented, but who also
want to support earlier Python versions, to simply write::

Expand Down Expand Up @@ -911,7 +942,7 @@ Enums
``typing_extensions`` emulates this value on versions of Python which do
not support :pep:`649` by returning the same value as for ``VALUE`` semantics.

.. attribute:: SOURCE
.. attribute:: STRING

Equal to 3. When :pep:`649` is implemented, this format will produce an annotation
dictionary where the values have been replaced by strings containing
Expand Down
Loading
Loading