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 ¶
Return whether the argument is the Annotated
special form.
>>> is_annotated(Annotated)
True
>>> is_annotated(Annotated[int, ...])
False
is_any ¶
Return whether the argument is the Any
special form.
>>> is_any(Any)
True
is_classvar ¶
Return whether the argument is the ClassVar
type qualifier.
>>> is_classvar(ClassVar)
True
>>> is_classvar(ClassVar[int])
>>> False
is_concatenate ¶
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 ¶
Return whether the argument is the Final
type qualifier.
>>> is_final(Final)
True
>>> is_final(Final[int])
False
is_generic ¶
Return whether the argument is the Generic
special form.
>>> is_generic(Generic)
True
>>> is_generic(Generic[T])
False
is_literal ¶
Return whether the argument is the Literal
special form.
>>> is_literal(Literal)
True
>>> is_literal(Literal["a"])
False
is_literalstring ¶
Return whether the argument is the LiteralString
special form.
>>> is_literalstring(LiteralString)
True
is_namedtuple ¶
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 ¶
Return whether the argument is the Never
special form.
>>> is_never(Never)
True
is_newtype ¶
Return whether the argument is a NewType
.
>>> UserId = NewType("UserId", int)
>>> is_newtype(UserId)
True
is_nodefault ¶
Return whether the argument is the NoDefault
sentinel object.
>>> is_nodefault(NoDefault)
True
is_noreturn ¶
Return whether the argument is the NoReturn
special form.
>>> is_noreturn(NoReturn)
True
>>> is_noreturn(Never)
False
is_notrequired ¶
Return whether the argument is the NotRequired
special form.
>>> is_notrequired(NotRequired)
True
is_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 ¶
Return whether the argument is the ReadOnly
special form.
>>> is_readonly(ReadOnly)
True
is_required ¶
Return whether the argument is the Required
special form.
>>> is_required(Required)
True
is_self ¶
Return whether the argument is the Self
special form.
>>> is_self(Self)
True
is_typealias ¶
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 ¶
Return whether the argument is the TypeGuard
special form.
>>> is_typeguard(TypeGuard)
True
is_typeis ¶
Return whether the argument is the TypeIs
special form.
>>> is_typeis(TypeIs)
True
is_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 ¶
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 ¶
Return whether the argument is the Unpack
special form.
>>> is_unpack(Unpack)
True
>>> is_unpack(Unpack[Ts])
False