unitxt.type_utils module

class unitxt.type_utils.NormalizedType(origin: None | type | TypeVar, args: tuple | frozenset = ())

Bases: tuple

Normalized type, made it possible to compare, hash between types.

args: tuple | frozenset

Alias for field number 1

origin: None | type | TypeVar

Alias for field number 0

unitxt.type_utils.eval_forward_ref(ref, forward_refs=None)

Eval forward_refs in all cPython versions.

unitxt.type_utils.get_args(type_) Tuple

Get type arguments with all substitutions performed.

For unions, basic simplifications used by Union constructor are performed.

Examples

Here are some code examples using get_args from the typing_utils module:

from typing_utils import get_args

# Examples of get_args usage
get_args(Dict[str, int]) == (str, int)  # True
get_args(int) == ()  # True
get_args(Union[int, Union[T, int], str][int]) == (int, str)  # True
get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])  # True
get_args(Callable[[], T][int]) == ([], int)  # True
unitxt.type_utils.get_origin(type_)

Get the unsubscripted version of a type.

This supports generic types, Callable, Tuple, Union, Literal, Final and ClassVar. Return None for unsupported types.

Examples

Here are some code examples using get_origin from the typing_utils module:

from typing_utils import get_origin

# Examples of get_origin usage
get_origin(Literal[42]) is Literal  # True
get_origin(int) is None  # True
get_origin(ClassVar[int]) is ClassVar  # True
get_origin(Generic) is Generic  # True
get_origin(Generic[T]) is Generic  # True
get_origin(Union[T, int]) is Union  # True
get_origin(List[Tuple[T, T]][int]) == list  # True
unitxt.type_utils.isoftype(object, type)

Checks if an object is of a certain typing type, including nested types.

This function supports simple types (like int, str), typing types (like List[int], Tuple[str, int], Dict[str, int]), and nested typing types (like List[List[int]], Tuple[List[str], int], Dict[str, List[int]]).

Parameters:
  • object – The object to check.

  • type – The typing type to check against.

Returns:

True if the object is of the specified type, False otherwise.

Return type:

bool

Examples: .. highlight:: python .. code-block:: python

isoftype(1, int) # True isoftype([1, 2, 3], typing.List[int]) # True isoftype([1, 2, 3], typing.List[str]) # False isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]]) # True

unitxt.type_utils.issubtype(left: None | type | TypeVar, right: None | type | TypeVar, forward_refs: dict | None = None) bool | None

Check that the left argument is a subtype of the right.

For unions, check if the type arguments of the left is a subset of the right. Also works for nested types including ForwardRefs.

Examples

Here are some code examples using issubtype from the typing_utils module:

from typing_utils import issubtype

# Examples of issubtype checks
issubtype(typing.List, typing.Any)  # True
issubtype(list, list)  # True
issubtype(list, typing.List)  # True
issubtype(list, typing.Sequence)  # True
issubtype(typing.List[int], list)  # True
issubtype(typing.List[typing.List], list)  # True
issubtype(list, typing.List[int])  # False
issubtype(list, typing.Union[typing.Tuple, typing.Set])  # False
issubtype(typing.List[typing.List], typing.List[typing.Sequence])  # True

# Example with custom JSON type
JSON = typing.Union[
    int, float, bool, str, None, typing.Sequence["JSON"],
    typing.Mapping[str, "JSON"]
]
issubtype(str, JSON, forward_refs={'JSON': JSON})  # True
issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON})  # True
issubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON})  # False
unitxt.type_utils.normalize(type_: None | type | TypeVar) NormalizedType

Convert types to NormalizedType instances.

unitxt.type_utils.optional_all(elements) bool | None
unitxt.type_utils.optional_any(elements) bool | None