nemo_microservices._utils._typing#

Module Contents#

Functions#

extract_type_arg

extract_type_var_from_base

Given a type like Foo[T], returns the generic type variable T.

is_annotated_type

is_iterable_type

If the given type is typing.Iterable[T]

is_list_type

is_required_type

is_sequence_type

is_type_alias_type

Return whether the provided argument is an instance of TypeAliasType.

is_typevar

is_union_type

strip_annotated_type

API#

nemo_microservices._utils._typing.extract_type_arg(typ: type, index: int) type#
nemo_microservices._utils._typing.extract_type_var_from_base(
typ: type,
*,
generic_bases: tuple[type, ...],
index: int,
failure_message: str | None = None,
) type#

Given a type like Foo[T], returns the generic type variable T.

This also handles the case where a concrete subclass is given, e.g.

class MyResponse(Foo[bytes]):
    ...

extract_type_var(MyResponse, bases=(Foo,), index=0) -> bytes

And where a generic subclass is given:

_T = TypeVar('_T')
class MyResponse(Foo[_T]):
    ...

extract_type_var(MyResponse[bytes], bases=(Foo,), index=0) -> bytes
nemo_microservices._utils._typing.is_annotated_type(typ: type) bool#
nemo_microservices._utils._typing.is_iterable_type(typ: type) bool#

If the given type is typing.Iterable[T]

nemo_microservices._utils._typing.is_list_type(typ: type) bool#
nemo_microservices._utils._typing.is_required_type(typ: type) bool#
nemo_microservices._utils._typing.is_sequence_type(typ: type) bool#
nemo_microservices._utils._typing.is_type_alias_type(
tp: Any,
/,
) typing_extensions.TypeIs[typing_extensions.TypeAliasType]#

Return whether the provided argument is an instance of TypeAliasType.

type Int = int
is_type_alias_type(Int)
# > True
Str = TypeAliasType("Str", str)
is_type_alias_type(Str)
# > True
nemo_microservices._utils._typing.is_typevar(typ: type) bool#
nemo_microservices._utils._typing.is_union_type(typ: type) bool#
nemo_microservices._utils._typing.strip_annotated_type(typ: type) type#