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 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
40 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
0f0f20d
comment update
Daraan Nov 27, 2024
8fdba74
Merge branch 'main' into 3.14/evaluate_forward_ref
Daraan Dec 13, 2024
b093a80
formating
Daraan Dec 14, 2024
c982e82
change variable usage
Daraan Dec 14, 2024
8cae202
Restructure test coverage
Daraan Dec 14, 2024
67059fd
Rework tests of nested strings
Daraan Dec 14, 2024
145e833
Merge remote-tracking branch 'upstream/main' into 3.14/evaluate_forwa…
Daraan Dec 14, 2024
a770495
Fix not using type_params in < 3.12.5
Daraan Dec 14, 2024
520b5cd
rework special cases and forward ref
Daraan Dec 14, 2024
ed4ec2d
Add ClassVar and constant check
Daraan Dec 14, 2024
85f1d3f
Explicit regex capture message
Daraan Dec 17, 2024
f995420
Add comment and and corrected regex
Daraan Dec 17, 2024
21e97c3
Merge branch 'main' into 3.14/evaluate_forward_ref
Daraan Jan 6, 2025
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.

# Release 4.12.2 (June 7, 2024)

Expand Down
31 changes: 31 additions & 0 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 Down
Loading
Loading