# Adding a \`typing.evaluate\_type()\` function

**URL:** <https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698>\
**Category:** Typing\
**Created:** [July 30, 2025, 6:25pm UTC](https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698 "2025-07-30T18:25:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [July 30, 2025, 6:25pm UTC](https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698/1 "2025-07-30T18:25:14Z")

</div>

We currently have two functions in the stdlib to evaluate type hints: [`typing.get_type_hints()`](https://docs.python.org/3.14/library/typing.html#typing.get_type_hints) and [`annotationlib.get_annotations()`](https://docs.python.org/3.14/library/annotationlib.html#annotationlib.get_annotations).

These two functions expect a function, module or class object to be passed, and so it is currently not possible to evaluate a bare annotation expression. As I mentioned [here](https://github.com/python/cpython/issues/136316#issuecomment-3041402581), it would be useful for a couple reasons:

- When evaluating annotations for a class/function (using the [`Format.VALUE`](https://docs.python.org/3.14/library/annotationlib.html#annotationlib.Format.VALUE) format), it is not possible to track which annotated attribute/function parameter failed to have its annotation evaluated. For runtime type checkers, this can be useful to indicate to end users where they should look to fix the error.
- Several runtime type checkers provide the ability to validate data against a bare annotation expression ([trycast](https://github.com/davidfstr/trycast), Pydantic’s [`TypeAdapter`](https://docs.pydantic.dev/dev/concepts/type_adapter/)).

I think it would be valuable to have a `typing.evaluate_type()` public function in the stdlib (or at least in `typing-extensions`):

```python
def evaluate_type(
    t,
    globalns=None,
    localns=None,
    format=None,
    owner=None,
):
    return _eval_type(t, globalns, localns, type_params=(), format=format, owner=owner)

```

That would delegate to the existing private `_eval_type()` function (already [relied on](https://github.com/search?q=lang%3Apython+%22typing._eval_type%28%22+NOT+is%3Afork&type=code) relatively heavily).

To be determined:

- should `type_params` be added as a parameter?
- here’s ongoing work for this already: [gh-85668: Create public API for typing.\_eval\_type by Dominik1123 · Pull Request #21753 · python/cpython · GitHub](https://github.com/python/cpython/pull/21753). It was questioned whether `globalns`/`localns` should have a default value. I think with the introduction of `owner` in 3.14, we can make them optional.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [July 31, 2025, 3:24am UTC](https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698/2 "2025-07-31T03:24:25Z")

</div>

I wonder whether there should be a `globalns` and `localns` argument at all. When would you use them over `owner`?

---

<div class="post-metadata">

**Author:** ![Daverball](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daverball/32/15931_2.png) [@Daverball](https://discuss.python.org/u/Daverball)\
**Post date:** [July 31, 2025, 7:00am UTC](https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698/3 "2025-07-31T07:00:55Z")

</div>

I think this can still be useful for something like SQLAlchemy’s ORM where it’s common with back references that you are unable to import one or more of the related models at class definition time. The mapper resolves those forward references later, once all the models have been registered. So in that sense there’s not a clear single owner of those symbols. You would need to create one, so it seems easier to just generate a `globalns`/`localns` that contains all the symbols it needs to contain\[1\].

It’s certainly the less common use-case, but I don’t think we need to remove it altogether, constructing a dummy owner seems more cumbersome than manipulating two dictionaries.

We certainly should steer users towards considering `owner` for their use-case before reaching for `globalns`/`localns`.

* * *

1. i.e. the ones it would contain based on where the annotation was defined and additionally all the models it knows about

---

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [July 31, 2025, 7:08am UTC](https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698/4 "2025-07-31T07:08:40Z")

</div>

Agree with what David said! Fwiw, in Pydantic, we need to be able to pass the globals and locals as we use slightly different logic to construct the globals and locals, that we can’t change for backwards compatibility.

We should probably emphasize on using _owner_ as the preferred approach in docs (and maybe add overloads to this function + the existing `evaluate_forward_ref()` to make _owner_ and _globalns_/_localns_ mutually exclusive).

---

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [May 24, 2026, 7:35pm UTC](https://discuss.python.org/t/adding-a-typing-evaluate-type-function/100698/5 "2026-05-24T19:35:35Z")

</div>

I opened [gh-85668: Add `typing.evaluate_type()` function by Viicos · Pull Request #150363 · python/cpython · GitHub](https://github.com/python/cpython/pull/150363).
