Skip to content

array_typing.typing

Functions:

Attributes:

Scalar module-attribute

Scalar = bool | int | float

StrPath module-attribute

StrPath = str | PathLike[str]

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_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_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_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)