Python Glossary Русский

Python 3.8+typingfunction

typing.get_args

introspection

Return the tuple of type arguments of a generic: get_args(list[int]) → (int,).

Syntax

typing.get_args(tp)

On a plain, unsubscripted type it returns an empty tuple: get_args(int) → (). It's typically used together with get_origin to pick an annotation apart at runtime; for Callable the first element is the whole argument list rather than separate types: get_args(Callable[[int], str]) → ([int], str).

Examples

# Python 3.9+
from typing import get_args, get_origin, Literal
print(get_args(list[int]) == (int,))   # → True
print([t.__name__ for t in get_args(dict[str, int])])   # → ['str', 'int']
print(get_args(Literal['a', 'b']))   # → ('a', 'b')
print(get_args(int))   # → ()
print(get_origin(list[int]))   # → <class 'list'>

See also

Open in the full glossarydocs.python.orgReport an inaccuracy