Skip to content

array_typing

Modules:

Functions:

Attributes:

ArrayLike module-attribute

ArrayLike = ArrayLike

Scalar module-attribute

Scalar = int | float | complex

ScalarLike module-attribute

ScalarLike = ScalarLike

as_dict_of_numpy

as_dict_of_numpy(
    obj: Mapping[str, ArrayLike] | None
) -> dict[str, ndarray]
Source code in src/array_typing/numpy/_utils/_as_numpy.py
19
20
21
22
def as_dict_of_numpy(obj: Mapping[str, at.ArrayLike] | None) -> dict[str, np.ndarray]:
    if obj is None:
        return {}
    return {k: at.as_numpy(v) for k, v in obj.items()}

as_numpy

as_numpy(obj: Any) -> ndarray
Source code in src/array_typing/numpy/_utils/_as_numpy.py
11
12
13
14
15
16
def as_numpy(obj: Any) -> np.ndarray:
    if at.is_numpy(obj):
        return obj
    if at.is_torch(obj):
        return obj.numpy(force=True)
    return np.asarray(obj)

as_scalar

as_scalar(x: ScalarLike) -> Scalar
Source code in src/array_typing/python/_cast.py
 4
 5
 6
 7
 8
 9
10
11
def as_scalar(x: at.ScalarLike) -> at.Scalar:
    if at.is_jax(x):
        return x.item()
    if at.is_numpy(x):
        return x.item()
    if at.is_torch(x):
        return x.item()
    return x  # pyright: ignore [reportReturnType]

flatten

flatten(
    iterable: (
        _T
        | Iterable[_T]
        | Iterable[Iterable[_T]]
        | Iterable
    ),
    base_type: tuple[type, ...] = (str, bytes),
) -> Iterable[_T]
Source code in src/array_typing/utils/_sequence.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
def flatten(
    iterable: _T | Iterable[_T] | Iterable[Iterable[_T]] | Iterable,
    base_type: tuple[type, ...] = (str, bytes),
) -> Iterable[_T]:
    if not at.is_iterable(iterable, base_type):
        yield iterable  # pyright: ignore [reportReturnType]
        return

    for item in iterable:
        if at.is_iterable(item, base_type):
            yield from flatten(item)
        else:
            yield item

full_name

full_name(obj: Any) -> str

Returns the fully qualified name of the given object.

Parameters:

  • obj (Any) –

    The object whose fully qualified name is to be returned.

Returns:

  • str

    The fully qualified name of the object.

Reference
  1. https://stackoverflow.com/a/2020083/18410348
Source code in src/array_typing/typing/_name.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def full_name(obj: Any) -> str:
    """Returns the fully qualified name of the given object.

    Args:
        obj: The object whose fully qualified name is to be returned.

    Returns:
        The fully qualified name of the object.

    Reference:
        1. <https://stackoverflow.com/a/2020083/18410348>
    """
    if not isinstance(obj, type):
        return full_name(type(obj))
    clazz: type = obj
    module: str = clazz.__module__
    if module == "builtins":
        return clazz.__qualname__
    return clazz.__module__ + "." + clazz.__qualname__

is_array_like

is_array_like(obj: Any) -> bool
Source code in src/array_typing/array_like/_utils.py
4
5
def is_array_like(obj: Any) -> bool:
    return hasattr(obj, "__len__") and not isinstance(obj, str | bytes)

is_class_named

is_class_named(
    cls: type, name: str | Sequence[str]
) -> bool
Source code in src/array_typing/typing/_name.py
28
29
def is_class_named(cls: type, name: str | Sequence[str]) -> bool:
    return any((full_name(cls) in at.flatten(name)) for cls in cls.__mro__)

is_class_named_partial

is_class_named_partial(
    cls: type, name: str | Sequence[str]
) -> bool
Source code in src/array_typing/typing/_name.py
32
33
34
35
36
37
38
39
def is_class_named_partial(cls: type, name: str | Sequence[str]) -> bool:
    for clazz in cls.__mro__:
        class_parts: list[str] = full_name(clazz).split(".")
        for n in at.flatten(name):
            name_parts: list[str] = n.split(".")
            if at.is_subsequence(name_parts, class_parts):
                return True
    return False

is_instance_named

is_instance_named(
    obj: Any, name: str | Sequence[str]
) -> bool
Source code in src/array_typing/typing/_name.py
42
43
def is_instance_named(obj: Any, name: str | Sequence[str]) -> bool:
    return is_class_named(type(obj), name)

is_instance_named_partial

is_instance_named_partial(
    obj: Any, name: str | Sequence[str]
) -> bool
Source code in src/array_typing/typing/_name.py
46
47
def is_instance_named_partial(obj: Any, name: str | Sequence[str]) -> bool:
    return is_class_named_partial(type(obj), name)

is_iterable

is_iterable(
    obj: Any, base_type: tuple[type, ...] = (str, bytes)
) -> TypeGuard[Iterable]
Source code in src/array_typing/typing/_is.py
5
6
7
8
def is_iterable(
    obj: Any, base_type: tuple[type, ...] = (str, bytes)
) -> TypeGuard[Iterable]:
    return isinstance(obj, Iterable) and not isinstance(obj, base_type)

is_jax

is_jax(obj: Any) -> TypeGuard[Array]
Source code in src/array_typing/jax/_utils.py
11
12
def is_jax(obj: Any) -> TypeGuard[jax.Array]:
    return at.is_instance_named_partial(obj, "jax.Array")

is_named

is_named(obj: Any, name: str | Sequence[str]) -> bool
Source code in src/array_typing/typing/_name.py
50
51
52
53
def is_named(obj: Any, name: str | Sequence[str]) -> bool:
    if isinstance(obj, type):
        return is_class_named(obj, name)
    return is_instance_named(obj, name)

is_named_partial

is_named_partial(
    obj: Any, name: str | Sequence[str]
) -> bool
Source code in src/array_typing/typing/_name.py
56
57
58
59
def is_named_partial(obj: Any, name: str | Sequence[str]) -> bool:
    if isinstance(obj, type):
        return is_class_named_partial(obj, name)
    return is_instance_named_partial(obj, name)

is_numpy

is_numpy(obj: Any) -> TypeGuard[ndarray]
Source code in src/array_typing/numpy/_utils/_is.py
10
11
def is_numpy(obj: Any) -> TypeGuard[np.ndarray]:
    return at.is_instance_named_partial(obj, "numpy.ndarray")

is_sequence

is_sequence(
    obj: Any, base_type: tuple[type, ...] = (str, bytes)
) -> TypeGuard[Sequence]
Source code in src/array_typing/typing/_is.py
11
12
13
14
def is_sequence(
    obj: Any, base_type: tuple[type, ...] = (str, bytes)
) -> TypeGuard[Sequence]:
    return isinstance(obj, Sequence) and not isinstance(obj, base_type)

is_subsequence

is_subsequence(a: Sequence[Any], b: Sequence[Any]) -> bool
Source code in src/array_typing/utils/_sequence.py
24
25
26
27
28
29
30
31
def is_subsequence(a: Sequence[Any], b: Sequence[Any]) -> bool:
    i: int = 0
    j: int = 0
    while i < len(a) and j < len(b):
        if a[i] == b[j]:
            i += 1
        j += 1
    return i == len(a)

is_torch

is_torch(obj: Any) -> TypeGuard[Tensor]
Source code in src/array_typing/torch/_utils.py
11
12
def is_torch(obj: Any) -> TypeGuard[torch.Tensor]:
    return at.is_instance_named_partial(obj, "torch.Tensor")