Skip to content

omnipy.components.general.models

CLASS DESCRIPTION
Chain2
Chain3
Chain4
Chain5
Chain6
GroupByTypeModel

Group list items by their runtime type.

HasOuterType
NotIterableExceptStrOrBytesModel

Model describing any object that is not iterable, except for str and bytes types.

Chain2

Bases: _ChainMixin, Model[_V | _U], Generic[_U, _V]

Source code in src/omnipy/components/general/models.py
class Chain2(_ChainMixin, Model[_V | _U], Generic[_U, _V]):
    ...

Chain3

Bases: _ChainMixin, Model[_W | TypeVarStore1[_V] | _U], Generic[_U, _V, _W]

Source code in src/omnipy/components/general/models.py
class Chain3(_ChainMixin, Model[_W | TypeVarStore1[_V] | _U], Generic[_U, _V, _W]):
    ...

Chain4

Bases: _ChainMixin, Model[_X | TypeVarStore2[_W] | TypeVarStore1[_V] | _U], Generic[_U, _V, _W, _X]

Source code in src/omnipy/components/general/models.py
class Chain4(_ChainMixin,
             Model[_X | TypeVarStore2[_W] | TypeVarStore1[_V] | _U],
             Generic[_U, _V, _W, _X]):
    ...

Chain5

Bases: _ChainMixin, Model[_Y | TypeVarStore3[_X] | TypeVarStore2[_W] | TypeVarStore1[_V] | _U], Generic[_U, _V, _W, _X, _Y]

Source code in src/omnipy/components/general/models.py
class Chain5(
        _ChainMixin,
        Model[_Y | TypeVarStore3[_X] | TypeVarStore2[_W] | TypeVarStore1[_V] | _U],
        Generic[_U, _V, _W, _X, _Y],
):
    ...

Chain6

Bases: _ChainMixin, Model[_Z | TypeVarStore4[_Y] | TypeVarStore3[_X] | TypeVarStore2[_W] | TypeVarStore1[_V] | _U], Generic[_U, _V, _W, _X, _Y, _Z]

Source code in src/omnipy/components/general/models.py
class Chain6(
        _ChainMixin,
        Model[_Z | TypeVarStore4[_Y] | TypeVarStore3[_X] | TypeVarStore2[_W] | TypeVarStore1[_V]
              | _U],
        Generic[_U, _V, _W, _X, _Y, _Z],
):
    ...

GroupByTypeModel

Bases: Chain2[Model[list], Model[dict[type | GenericAlias, list]]]

Group list items by their runtime type.

The model converts a list into a dictionary mapping each inferred item type to the sublist of items having that type. For mappings and other non-string iterables, it attempts to preserve more detailed generic type information, such as key/value types for mappings and element types for tuples and other iterables, when those type forms can be constructed at runtime.

Examples:

>>> GroupByTypeModel([1, 'a', 2, [3], ['b']]).to_data()
{int: [1, 2], str: ['a'], list[int]: [[3]], list[str]: [['b']]}
Source code in src/omnipy/components/general/models.py
class GroupByTypeModel(Chain2[Model[list], Model[dict[type | GenericAlias, list]]]):
    """
    Group list items by their runtime type.

    The model converts a list into a dictionary mapping each inferred item
    type to the sublist of items having that type. For mappings and other
    non-string iterables, it attempts to preserve more detailed generic
    type information, such as key/value types for mappings and element
    types for tuples and other iterables, when those type forms can be
    constructed at runtime.

    Examples:
        >>> GroupByTypeModel([1, 'a', 2, [3], ['b']]).to_data()
        {int: [1, 2], str: ['a'], list[int]: [[3]], list[str]: [['b']]}
    """
    @classmethod
    def _parse_data(cls, data: Model[list]) -> Model[dict[type | GenericAlias, list]]:
        grouped: dict[type, list] = defaultdict(list)

        def _iter_union_type(seq: Iterable):
            return Union[tuple(type(item) for item in seq)]

        def _deduce_full_type(_item: object) -> type:
            try:
                if isinstance(_item, Mapping):
                    return type(_item)[  # type: ignore[index]
                        _iter_union_type(_item.keys()),
                        _iter_union_type(_item.values()),
                    ]
                elif isinstance(_item, tuple):
                    return tuple[tuple(type(_) for _ in _item)]
                elif is_non_str_byte_iterable(_item):
                    return type(_item)[_iter_union_type(_item)]  # type: ignore[index]
            except TypeError:
                pass
            return type(_item)

        for item in data.content:
            full_type = _deduce_full_type(item)
            grouped[full_type].append(item)  # pyright: ignore [reportArgumentType]
        return Model[dict[type | GenericAlias, list]](grouped)

HasOuterType

Bases: Protocol

METHOD DESCRIPTION
outer_type
Source code in src/omnipy/components/general/models.py
class HasOuterType(Protocol):
    @classmethod
    def outer_type(cls, with_args: bool = False) -> TypeForm:
        ...

outer_type classmethod

outer_type(with_args: bool = False) -> TypeForm
Source code in src/omnipy/components/general/models.py
@classmethod
def outer_type(cls, with_args: bool = False) -> TypeForm:
    ...

NotIterableExceptStrOrBytesModel

Bases: Model[object | None]

Model describing any object that is not iterable, except for str and bytes types. As strings and bytes are iterable (over the characters/bytes) but also generally useful and often considered singular (or scalar) types, they are specifically allowed by this model.

Examples:

>>> from omnipy import NotIterableExceptStrOrBytesModel, print_exception
>>>
>>> NotIterableExceptStrOrBytesModel(1234)
NotIterableExceptStrOrBytesModel(1234)
>>> NotIterableExceptStrOrBytesModel('1234')
NotIterableExceptStrOrBytesModel(1234)
>>> with print_exception:
...     NotIterableExceptStrOrBytesModel((1, 2, 3, 4))
ValidationError: 1 validation error for NotIterableExceptStrOrBytesModel
Note

JsonScalarModel is a strict submodel of NotIterableExceptStrOrBytesModel in that all objects allowed by JsonScalarModel are also allowed by NotIterableExceptStrOrBytesModel.

Source code in src/omnipy/components/general/models.py
class NotIterableExceptStrOrBytesModel(Model[object | None]):
    """
    Model describing any object that is not iterable, except for `str` and `bytes` types.
    As strings and bytes are iterable (over the characters/bytes) but also generally useful and
    often considered singular (or scalar) types, they are specifically allowed by this model.

    Examples:
        >>> from omnipy import NotIterableExceptStrOrBytesModel, print_exception
        >>>
        >>> NotIterableExceptStrOrBytesModel(1234)
        NotIterableExceptStrOrBytesModel(1234)
        >>> NotIterableExceptStrOrBytesModel('1234')
        NotIterableExceptStrOrBytesModel(1234)
        >>> with print_exception:
        ...     NotIterableExceptStrOrBytesModel((1, 2, 3, 4))
        ValidationError: 1 validation error for NotIterableExceptStrOrBytesModel

    Note:
        JsonScalarModel is a strict submodel of NotIterableExceptStrOrBytesModel in that all objects
        allowed by JsonScalarModel are also allowed by NotIterableExceptStrOrBytesModel.
    """
    @classmethod
    def _parse_data(cls, data: object) -> object:
        if isinstance(data, NotIterableExceptStrOrBytesModel):
            return data

        assert isinstance(data, str) or isinstance(data, bytes) or not is_iterable(data), \
            f'Data of type {type(data)} is iterable'

        return data