bpack.typing module

bpack support for type annotations.

class bpack.typing.T(*args, **kwargs)[source]

Bases: object

Allow to specify numeric type annotations using string descriptors.

Example:

>>> T['u4']                           
typing.Annotated[int, TypeParams(byteorder=None,
                 type='int', size=4, signed=False)]

The resulting type annotation is a typing.Annotated numeric type with attached a bpack.typing.TypeParams instance.

String descriptors, or typestr, are compatible with numpy (a sub-set of one used in the numpy “array interface”).

The typestr string format consists of 3 parts:

  • an (optional) character describing the byte order of the data

    • <: little-endian,

    • >: big-endian,

    • |: not-relevant

  • a character code giving the basic type of the array, and

  • an integer providing the number of bytes the type uses

The basic type character codes are:

  • i: sighed integer

  • u: unsigned integer

  • f: float

  • c: complex

  • S: bytes (string)

Note

typestr the format described above is a sub-set of the one used in the numpy “array interface”.

class bpack.typing.TypeParams(byteorder: EByteOrder | None, type: type[bool | int | float | complex | bytes | str], size: int | None, signed: bool | None)[source]

Bases: NamedTuple

Named tuple describing type parameters.

byteorder: EByteOrder | None

Alias for field number 0

signed: bool | None

Alias for field number 3

size: int | None

Alias for field number 2

type: type[bool | int | float | complex | bytes | str]

Alias for field number 1

bpack.typing.is_annotated(type_: type) bool[source]

Return True if the input is an annotated numeric type.

An annotated numeric type is assumed to be a typing.Annotated type annotation of a basic numeric type with attached a bpack.typing.TypeParams instance.

See also

bpack.typing.T.