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, type, size, signed)[source]

Bases: tuple

property byteorder

Alias for field number 0

property signed

Alias for field number 3

property size

Alias for field number 2

property type

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.