gh-85668: Add typing.evaluate_type() function#150363
Open
Viicos wants to merge 1 commit into
Open
Conversation
Documentation build overview
|
Viicos
commented
May 24, 2026
| ) | ||
|
|
||
|
|
||
| def evaluate_type( |
Contributor
Author
There was a problem hiding this comment.
Should we allow plain strings to be passed, by using _make_forward_ref()? If so, what should we use for is_argument and is_class? I'd suggest we can infer the object type from owner, and if globals/locals/type params are manually provided instead, we pass is_argument=False, is_class=True as we can't assume anything on the owner and so we allow any type qualifier.
Viicos
commented
May 24, 2026
| This is similar to calling :func:`evaluate_forward_ref`, but unlike that | ||
| method, :func:`evaluate_type` also supports arbitrary type hints. | ||
|
|
||
| See the documentation for :meth:`annotationlib.ForwardRef.evaluate` for |
Contributor
Author
There was a problem hiding this comment.
We can also properly document the behavior of each argument here, but this will introduce some repetition..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As per the discussion in https://discuss.python.org/t/100698.
With the new stuff since 3.14, we end up in a bit of an unfortunate situation, where we already have
typing.evaluate_forward_ref()as a public function that can do pretty much anything, expect it requires a forward ref instance. So with this PR we end up with the chainevaluate_type()which is likeevaluate_forward_ref()but allows arbitrary type hints, andevaluate_forward_ref()being likeannotationlib.ForwardRef.evaluate()but also recursively evaluates nested forward references; this may be a bit confusing for users.Once we agree on the expected behavior and signature for this new function, I'll add tests (wondering if I should keep the scope narrow,
_eval_type()is already extensively tested byget_type_hints()) and flesh out the code docstring of the function.