Registry / serialization / typing-inspect

typing-inspect

JSON →
library0.9.0pypypi✓ verified 26d ago

typing-inspect is a Python library providing runtime inspection utilities for types defined in the standard `typing` module. It is currently at version 0.9.0, is actively maintained, and compatible with Python 3.6 and later. It offers an easy-to-use API for advanced introspection of type hints.

pip install typing-inspect
INSTALL
IMPORT
SIG · TYPING-INSPECT
T
typing-inspect
serializationpythonv0.9.0
Install
1.8s avg
Import
41ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.042s · 18.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.040s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

is_generic_type
from typing_inspect import is_generic_type
get_args
from typing_inspect import get_args
get_origin
from typing_inspect import get_origin
is_union_type
from typing_inspect import is_union_type
is_optional_type
from typing_inspect import is_optional_type

This quickstart demonstrates basic usage of `typing-inspect` to check for generic types, retrieve type arguments and origins, and identify optional types. It covers `is_generic_type`, `get_args`, `get_origin`, and `is_optional_type`.

from typing import List, Union, Optional, TypeVar from typing_inspect import is_generic_type, get_args, get_origin, is_optional_type T = TypeVar('T') class MyGeneric(List[T]): pass # Example 1: Check if a type is generic assert is_generic_type(MyGeneric[int]) # This should be True assert not is_generic_type(int) # This should be False # Example 2: Get arguments of a type args = get_args(List[str]) assert args == (str,) union_type = Union[int, str] union_args = get_args(union_type) assert set(union_args) == {int, str} # Example 3: Get the origin of a type origin = get_origin(List[int]) assert origin is list # Example 4: Check for optional types assert is_optional_type(Optional[int]) assert not is_optional_type(int) print("All quickstart assertions passed!")
Debug
Known issues
breakingThe API of `typing-inspect` is still considered experimental. While generally stable, breaking changes might occur in future versions. Users should consult the changelog for specific updates.
fix
Always pin the `typing-inspect` version in your `requirements.txt` or `pyproject.toml` to ensure consistent behavior across deployments.
affects: All versions
gotchaThere exists another, distinct library named `typing-inspection` (used by Pydantic) which provides similar runtime typing introspection tools. Ensure you are importing from `typing_inspect` (underscore) for this library, not `typing_inspection` (hyphen).
fix
Double-check your `pip install` command (`pip install typing-inspect`) and import statements (`import typing_inspect` or `from typing_inspect import ...`) to ensure you are using the correct package.
affects: All versions
gotchaThe `get_args` function, when called with `evaluate=True`, can be computationally expensive in terms of both time and memory. This option performs all type parameter substitutions.
fix
Avoid using `get_args(tp, evaluate=True)` in performance-critical paths unless strictly necessary. The default `evaluate=False` reports results as nested tuples and is generally more performant.
affects: All versions
gotcha`typing-inspect` is designed to work with the latest versions of the Python `typing` module and `typing-extensions`. Older or specific versions of these dependencies might lead to unexpected behavior or unsupported types.
fix
Ensure your project's `typing` and `typing-extensions` dependencies are up-to-date or meet the minimum versions specified by `typing-inspect` (currently `typing>=3.7.4` and `typing-extensions>=3.7.4`).
affects: All versions
Upgrade
Version history
0.9.0latest on PyPI · released May 24, 2023
Audit
Dependencies
mypy-extensionsrequiredRequired for extended type functionality.
typing-extensionsrequiredRequired for backporting new typing features to older Python versions.
typingrequiredRequired for core typing module functionality; specifically, this library works with typing version 3.7.4 and later.
Agent activity
9 hits · last 30 days
node
6
Amazon
1
OpenAI (training)
1
Resources