Skip to content

typing_objects

Low-level introspection utilities for typing members.

The provided functions in this module check against both the typing and typing_extensions variants, if they exists and are different.

is_annotated

is_annotated(obj: Any) -> bool

Return whether the argument is the Annotated special form.

>>> is_annotated(Annotated)
True
>>> is_annotated(Annotated[int, ...])
False

is_any

is_any(obj: Any) -> bool

Return whether the argument is the Any special form.

>>> is_any(Any)
True

is_classvar

is_classvar(obj: Any) -> bool

Return whether the argument is the ClassVar type qualifier.

>>> is_classvar(ClassVar)
True
>>> is_classvar(ClassVar[int])
>>> False

is_concatenate

is_concatenate(obj: Any) -> bool

Return whether the argument is the Concatenate special form.

>>> is_concatenate(Concatenate)
True
>>> is_concatenate(Concatenate[int, P])
False

is_deprecated

is_deprecated(obj: Any) -> TypeIs[deprecated]

Return whether the argument is a deprecated instance.

This also includes the typing_extensions backport.

>>> is_deprecated(warnings.deprecated('message'))
True
>>> is_deprecated(typing_extensions.deprecated('deprecated'))
True

is_final

is_final(obj: Any) -> bool

Return whether the argument is the Final type qualifier.

>>> is_final(Final)
True
>>> is_final(Final[int])
False

is_generic

is_generic(obj: Any) -> bool

Return whether the argument is the Generic special form.

>>> is_generic(Generic)
True
>>> is_generic(Generic[T])
False

is_literal

is_literal(obj: Any) -> bool

Return whether the argument is the Literal special form.

>>> is_literal(Literal)
True
>>> is_literal(Literal["a"])
False

is_literalstring

is_literalstring(obj: Any) -> bool

Return whether the argument is the LiteralString special form.

>>> is_literalstring(LiteralString)
True

is_namedtuple

is_namedtuple(obj: Any) -> bool

Return whether the argument is a named tuple type.

This includes NamedTuple subclasses and classes created from the collections.namedtuple factory function.

>>> class User(NamedTuple):
...     name: str
...
>>> is_namedtuple(User)
True
>>> City = collections.namedtuple('City', [])
>>> is_namedtuple(City)
True
>>> is_namedtuple(NamedTuple)
False

is_never

is_never(obj: Any) -> bool

Return whether the argument is the Never special form.

>>> is_never(Never)
True

is_newtype

is_newtype(obj: Any) -> TypeIs[NewType]

Return whether the argument is a NewType.

>>> UserId = NewType("UserId", int)
>>> is_newtype(UserId)
True

is_nodefault

is_nodefault(obj: Any) -> bool

Return whether the argument is the NoDefault sentinel object.

>>> is_nodefault(NoDefault)
True

is_noreturn

is_noreturn(obj: Any) -> bool

Return whether the argument is the NoReturn special form.

>>> is_noreturn(NoReturn)
True
>>> is_noreturn(Never)
False

is_notrequired

is_notrequired(obj: Any) -> bool

Return whether the argument is the NotRequired special form.

>>> is_notrequired(NotRequired)
True

is_paramspec

is_paramspec(obj: Any) -> TypeIs[ParamSpec]

Return whether the argument is an instance of ParamSpec.

>>> P = ParamSpec('P')
>>> is_paramspec(P)
True

is_paramspecargs

is_paramspecargs(obj: Any) -> TypeIs[ParamSpecArgs]

Return whether the argument is an instance of ParamSpecArgs.

>>> P = ParamSpec('P')
>>> is_paramspecargs(P.args)
True

is_paramspeckwargs

is_paramspeckwargs(obj: Any) -> TypeIs[ParamSpecKwargs]

Return whether the argument is an instance of ParamSpecKwargs.

>>> P = ParamSpec('P')
>>> is_paramspeckwargs(P.kwargs)
True

is_readonly

is_readonly(obj: Any) -> bool

Return whether the argument is the ReadOnly special form.

>>> is_readonly(ReadOnly)
True

is_required

is_required(obj: Any) -> bool

Return whether the argument is the Required special form.

>>> is_required(Required)
True

is_self

is_self(obj: Any) -> bool

Return whether the argument is the Self special form.

>>> is_self(Self)
True

is_typealias

is_typealias(obj: Any) -> bool

Return whether the argument is the TypeAlias special form.

>>> is_typealias(TypeAlias)
True

is_typealiastype

is_typealiastype(obj: Any) -> TypeIs[TypeAliasType]

Return whether the argument is a TypeAliasType instance.

>>> type MyInt = int
>>> is_typealiastype(MyInt)
True
>>> MyStr = TypeAliasType("MyStr", str)
>>> is_typealiastype(MyStr):
True
>>> type MyList[T] = list[T]
>>> is_typealiastype(MyList[int])
False

is_typeguard

is_typeguard(obj: Any) -> bool

Return whether the argument is the TypeGuard special form.

>>> is_typeguard(TypeGuard)
True

is_typeis

is_typeis(obj: Any) -> bool

Return whether the argument is the TypeIs special form.

>>> is_typeis(TypeIs)
True

is_typevar

is_typevar(obj: Any) -> TypeIs[TypeVar]

Return whether the argument is an instance of TypeVar.

>>> T = TypeVar('T')
>>> is_typevar(T)
True

is_typevartuple

is_typevartuple(obj: Any) -> TypeIs[TypeVarTuple]

Return whether the argument is an instance of TypeVarTuple.

>>> Ts = TypeVarTuple('Ts')
>>> is_typevartuple(Ts)
True

is_union

is_union(obj: Any) -> bool

Return whether the argument is the Union special form.

This function can also be used to check for the Optional special form, as at runtime, Optional[int] is equivalent to Union[int, None].

>>> is_union(Union)
True
>>> is_union(Union[int, str])
False

Warning

This does not check for unions using the new syntax (e.g. int | str).

is_unpack

is_unpack(obj: Any) -> bool

Return whether the argument is the Unpack special form.

>>> is_unpack(Unpack)
True
>>> is_unpack(Unpack[Ts])
False

DEPRECATED_ALIASES

DEPRECATED_ALIASES: Final[dict[Any, type[Any]]]

A mapping between the deprecated typing aliases to their replacement, as per PEP 585.