nat.utils.type_utils#
Attributes#
Classes#
Descriptor that caches a computed value per-instance while remaining read-only. |
|
Functions#
|
|
|
Module Contents#
- type StrPath = str | os.PathLike[str]#
- type ClassInfo = type | types.UnionType | tuple['ClassInfo', ...]#
- is_valid_json(string)#
- override(func)#
- class read_only_cached_property(func: collections.abc.Callable[[Any], Any])#
Descriptor that caches a computed value per-instance while remaining read-only.
Unlike
functools.cached_property, this descriptor rejects attribute assignment, preserving the read-only contract of the original@propertyit replaces, while still avoiding the strong-reference memory leak caused byfunctools.lru_cacheon instance methods.- func#
- attrname#
- class DecomposedType(original: type)#
- type#
- origin()#
Get the origin of the current type using
typing.get_origin. For example, if the current type islist[int], the origin would belist.Returns#
- type
The origin of the current type.
- args()#
Get the arguments of the current type using
typing.get_args. For example, if the current type islist[int, str], the arguments would be[int, str].Returns#
- tuple[type]
The arguments of the current type.
- 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.
- is_empty()#
Check if the current type is eqivalent to
NoneType.Returns#
- bool
True if the current type is
NoneType, False otherwise.
- is_class()#
Check if the current type is a class using
inspect.isclass. For example,list[int]would return False, butlistwould return True.Returns#
- bool
True if the current type is a class, False otherwise.
- is_generic()#
Check if the current type is a generic using
typing.GenericMeta. For example,list[int]would return True, butlistwould return False.Returns#
- bool
True if the current type is a generic, False otherwise.
- is_annotated()#
Check if the current type is an annotated type using
typing.Annotated. For example,Annotated[int, str]would return True, butintwould return False.Returns#
- bool
True if the current type is an annotated type, False otherwise.
- is_union()#
Check if the current type is a union type using
typing.Union. For example,Union[int, str]would return True, butintwould return False.Returns#
- bool
True if the current type is a union type, False otherwise.
- is_async_generator()#
Check if the current type is an async generator type. For example,
AsyncGenerator[int]would return True, butintwould return False.Returns#
- bool
True if the current type is an async generator type, False otherwise.
- is_optional()#
Check if the current type is an optional type. For example,
Optional[int]andint | Nonewould return True, butintwould return False.Returns#
- bool
True if the current type is an optional type, False otherwise.
- 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 aValueError.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,
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( ) 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