# Typing-inspection: A library to inspect type annotations at runtime

**URL:** <https://discuss.python.org/t/typing-inspection-a-library-to-inspect-type-annotations-at-runtime/81301>\
**Category:** Typing\
**Created:** [February 19, 2025, 12:13pm UTC](https://discuss.python.org/t/typing-inspection-a-library-to-inspect-type-annotations-at-runtime/81301 "2025-02-19T12:13:34Z")\
**Posts on this page:** 1\
**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:** [February 19, 2025, 12:13pm UTC](https://discuss.python.org/t/typing-inspection-a-library-to-inspect-type-annotations-at-runtime/81301/1 "2025-02-19T12:13:34Z")

</div>

Inspecting type annotations at runtime can be hard. Often times, typing members aren’t properly checked, and the stdlib `get_origin()`/`get_args()` utility functions are not understood correctly.

Here are a couple examples:

- For a typing object named `TypingObj`, it is common to see simple `var is typing.TypingObj` checks in the wild, which do not account for the possibly different `typing_extensions` variant \[1\].
- `get_origin()` is not necessarily returning the “unparameterized” version of a generic typing object. For instance, `get_origin(typing.Tuple)` is `tuple`, while `get_origin(tuple)` is `None`.

Additionally, such checks can be become expensive if repeated a lot (`get_origin/args()` perform a lot of `isinstance` calls).

As per a [discussion](https://github.com/beartype/beartype/discussions/479) initiated by [@leyec](https://github.com/leycec), we created the [`typing-inspection` library](https://github.com/pydantic/typing-inspection) in the Pydantic org \[2\]. It provides:

- a low-level [`typing_objects` module](https://pydantic.github.io/typing-inspection/dev/api/typing_objects/), meant to check if a variable is a typing object, e.g. `is_annotated()`.
- a high-level [`introspection` module](https://pydantic.github.io/typing-inspection/dev/api/introspection/), meant to inspect type annotations (unpacking literal values, type qualifiers, etc).

To better understand how these utilities can be used, I wrote a (probably still incomplete) guide giving a recommended workflow to inspect type annotations, available [here](https://typing-inspection.pydantic.dev).

We are planning on making an initial release, soon, but we’d be pleased to have feedback from developers making use of type hints at runtime. Some design decisions are still under discussion, and as such I opened a bunch of [open issues](https://github.com/pydantic/typing-inspection/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22open%20issues%22) to discuss.

* * *

1. For instance, on Python 3.9, `get_origin(typing.Literal[1]) != get_origin(typing_extensions.Literal[1])`. 

2. Documentation is temporarily published on Github pages, but is subject to change.
