bpack.utils module

Utility functions and classes.

bpack.utils.classdecorator(func)[source]

Class decorator that can be used with or without parameters.

bpack.utils.create_fn(name, args, body, *, globals=None, locals=None, return_type=<dataclasses._MISSING_TYPE object>)[source]

Create a function object.

bpack.utils.effective_type(type_: Type | Type[Enum], keep_annotations: bool = False) Type[source]

Return the effective type.

In case of enums or sequences return the item type.

bpack.utils.enum_item_type(enum_cls: Type[Enum]) Type[source]

Return the type of the items of an enum.Enum.

This function also checks that all items of an enum have the same (or compatible) type.

bpack.utils.is_enum_type(type_: Type) bool[source]

Return True if the input is and enum.Enum.

bpack.utils.is_int_type(type_: Type) bool[source]

Return true if the effective type is an integer.

bpack.utils.is_sequence_type(type_: Type, error: bool = False) bool[source]

Return True if the input is an homogeneous typed sequence.

Please note that fields annotated with typing.Tuple are not considered homogeneous sequences even if all items are specified to have the same type.

bpack.utils.sequence_type(type_: Type, error: bool = False) Type | None[source]

Return the sequence type associated to a typed sequence.

The function return list or tuple if the input is considered a valid typed sequence, None otherwise.

Please note that fields annotated with typing.Tuple are not considered homogeneous sequences even if all items are specified to have the same type.

bpack.utils.set_new_attribute(cls, name, value)[source]

Programmatically add a new attribute/method to a class.