nat.utils.type_utils#

Attributes#

Classes#

Functions#

is_valid_json(string)

override(func)

Module Contents#

type StrPath = str | os.PathLike[str]#
type ClassInfo = type | types.UnionType | tuple['ClassInfo', ...]#
is_valid_json(string)#
override(func)#
class DecomposedType(original: type)#
type#
property origin#

Get the origin of the current type using typing.get_origin. For example, if the current type is list[int], the origin would be list.

Returns#

type

The origin of the current type.

property args#

Get the arguments of the current type using typing.get_args. For example, if the current type is list[int, str], the arguments would be [int, str].

Returns#

tuple[type]

The arguments of the current type.

property root#

Get the root type of the current type. This is the type without any annotations or async generators.

Returns#

type

The root type of the current type.

property is_empty#

Check if the current type is eqivalent to NoneType.

Returns#

bool

True if the current type is NoneType, False otherwise.

property is_class#

Check if the current type is a class using inspect.isclass. For example, list[int] would return False, but list would return True.

Returns#

bool

True if the current type is a class, False otherwise.

property is_generic#

Check if the current type is a generic using typing.GenericMeta. For example, list[int] would return True, but list would return False.

Returns#

bool

True if the current type is a generic, False otherwise.

property is_annotated#

Check if the current type is an annotated type using typing.Annotated. For example, Annotated[int, str] would return True, but int would return False.

Returns#

bool

True if the current type is an annotated type, False otherwise.

property is_union#

Check if the current type is a union type using typing.Union. For example, Union[int, str] would return True, but int would return False.

Returns#

bool

True if the current type is a union type, False otherwise.

property is_async_generator#

Check if the current type is an async generator type. For example, AsyncGenerator[int] would return True, but int would return False.

Returns#

bool

True if the current type is an async generator type, False otherwise.

property is_optional#

Check if the current type is an optional type. For example, Optional[int] and int | None would return True, but int would return False.

Returns#

bool

True if the current type is an optional type, False otherwise.

property has_base_type#

Check if the current type has a base type, ignoring any annotations or async generators.

get_optional_type() DecomposedType#

If the current type is optional, return the type that is not NoneType. If the current type is not optional, raise a ValueError.

Returns#

DecomposedType

The optional type that is not NoneType.

Raises#

ValueError

If the current type is not optional.

ValueError

If the current type is optional but has more than one argument that is not NoneType.

get_annotated_type() DecomposedType#

If the current type is annotated, return the annotated type. If the current type is not annotated, raise a ValueError.

Returns#

DecomposedType

The annotated type.

Raises#

ValueError

If the current type is not annotated.

get_async_generator_type() DecomposedType#

If the current type is an async generator, return the async generator type. If the current type is not an async generator, raise a ValueError.

Returns#

DecomposedType

The async generator type.

Raises#

ValueError

If the current type is not an async generator.

get_base_type() DecomposedType#

Returns the base type of the current type, ignoring any annotations or async generators.

Returns#

DecomposedType

The base type of the current type.

is_subtype(class_or_tuple: ClassInfo) bool#

Check if the current type is a subtype of the specified class or tuple of classes similar to issubclass.

Parameters#

class_or_tupleClassInfo

The class or tuple of classes to check if the current type is a subtype of.

Returns#

bool

True if the current type is a subtype of the specified class or tuple of classes, False otherwise

is_instance(instance: Any) bool#

Check if the current type is an instance of the specified instance similar to isinstance.

Parameters#

instancetyping.Any

The instance to check if the current type is an instance of.

Returns#

bool

True if the current type is an instance of the specified instance, False otherwise

get_pydantic_schema(
converters: list[collections.abc.Callable] | None = None,
) type[pydantic.BaseModel] | type[None]#

Get the Pydantic schema for the current type.

Parameters#

converterslist[Callable], optional

A list of converters to append new converts to, by default None

Returns#

type[BaseModel]

The Pydantic schema for the current type.

static extract_generic_parameters_from_class(
target_class: type,
expected_param_count: int | None = None,
) tuple[type, Ellipsis]#

Extract generic type parameters from a class’s inheritance chain.

This method searches through __orig_bases__ to find generic parameters, which is useful for classes that inherit from generic base classes.

Parameters#

target_classtype

The class to extract parameters from

expected_param_countint | None, optional

Expected number of parameters. If specified, only matches with this count are considered.

Returns#

tuple[type, …]

Tuple of generic type parameters found

Raises#

ValueError

If no generic parameters matching the expected count are found

Examples#

>>> class MyClass(SomeGeneric[int, str, bool]):
...     pass
>>> DecomposedType.extract_generic_parameters_from_class(MyClass, 3)
(int, str, bool)
static is_type_compatible(source_type: type, target_type: type) bool#

Check if a source type is compatible with a target type.

This handles direct compatibility and special cases like batch compatibility where list[T] can be compatible with targets that expect T.

Parameters#

source_typetype

The source type to check

target_typetype

The target type to check compatibility with

Returns#

bool

True if types are compatible, False otherwise