Skip to content

Simplify typing.evaluate_forward_ref #133960

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

Open
JelleZijlstra opened this issue May 13, 2025 · 0 comments
Open

Simplify typing.evaluate_forward_ref #133960

JelleZijlstra opened this issue May 13, 2025 · 0 comments
Labels
3.14 bugs and security fixes 3.15 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented May 13, 2025

Bug report

In PEP-749 I added typing.evaluate_forward_ref to replace the private typing.ForwardRef._evaluate, which is being used by some external users.

The current documentation claims these differences from annotationlib.ForwardRef.evaluate:

  1. Recursively evaluates forward references nested within the type hint.
  2. Raises TypeError when it encounters certain objects that are not valid type hints.
  3. Replaces type hints that evaluate to None with types.NoneType.
  4. Supports the FORWARDREF and STRING formats.

(1) is useful and fits well with the typing module; annotationlib can't do this because it requires introspecting into typing-specific objects. (2) I feel is not useful (compare #133959): the type check is not particularly thorough, and it's generally better for callers to allow more objects through that callers can handle on their own terms. (3) is sort of harmless but not particularly useful. (4) is not true any more since I also added support for these formats to ForwardRef.evaluate.

So I'd like to drop differences 2 through 4, leaving the function focused on recursively evaluating nested ForwardRefs.

Linked PRs

@JelleZijlstra JelleZijlstra added type-bug An unexpected behavior, bug, or error topic-typing 3.14 bugs and security fixes 3.15 new features, bugs and security fixes labels May 13, 2025
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 13, 2025
As explained in python#133960, this removes most of the behavior differences with ForwardRef.evaluate.
The remaining difference is about recursive evaluation of forwardrefs; this is practically useful
in cases where an annotation refers to a type alias that itself is string-valued.

This also improves several edge cases that were previously not handled optimally. For example,
the function now takes advantage of the partial evaluation behavior of ForwardRef.evaluate() to
evaluate more ForwardRefs in the FORWARDREF format.

This also fixes python#133959 as a side effect, because the buggy behavior in python#133959 derives from
evaluate_forward_ref().
@picnixz picnixz added the stdlib Python modules in the Lib dir label May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 bugs and security fixes 3.15 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants