Extension for the python package 'construct' that adds typing features
Project description
construct-typing
This project is an extension of the python package construct, which is a powerful declarative and symmetrical parser and builder for binary data. This Repository consists of two packages:
- construct-stubs: Adding .pyi for the whole construct 2.10 package (according to PEP 561 stub-only packages)
- construct_typed: Adding additional classes that help with autocompletion and additional type hints.
Installation
This package comply to PEP 561. So most of the static code analysers will recognise the stubs automatically. The installation only requires:
pip install construct-typing
Tests
The stubs are tested against the pytests of the construct package in a slightly modified form. Since the tests are relatively detailed I think most cases are covered.
The new typed constructs have new written pytests, which also passes all pytests and the static type checkers.
The following static type checkers are fully supported:
- mypy (
mypy --strict tests
) - pyright (
pyright tests
) (TODO: Some errors in pyright have to be fixed first)
Explanation
Stubs
The construct-stubs package is used for creating type hints for the orignial construct package. In particular the build
and parse
methods get type hints. So the core of the stubs are the TypeVar
's ParsedType
and BuildTypes
:
Construct.build
: converts an object of one of the types defined byBuildTypes
to abytes
object.Construct.parse
: converts abytes
object to an object of typeParsedType
.
For each of the Construct
s in the stubs it is defined which type it parses to and from which it can be build. For example:
Construct | parses to (ParsedType) | builds from (BuildTypes) |
---|---|---|
Int16ub |
int |
int |
Bytes |
bytes |
bytes , bytearray or memoryview |
Array(5, Int16ub) |
ListContainer[int] |
typing.List[int] |
Struct("i" / Byte) |
Container[typing.Any] |
Dict[str, typing.Any] or None |
The problem is to describe the more complex constructs like:
Sequence
,FocusedSeq
which has heterogenous subcons in comparison to anArray
with only homogenous subcons.Struct
,BitStruct
,LazyStruct
,Union
which has heterogenous and named subcons.
Currently only the very unspecific type typing.Any
can be used as type hint (maybe in the future it can be optimised a little, when variadic generics become available). But the biggest disadvantage is that autocompletion for the named subcons is not available.
Note: The stubs are based on construct in Version 2.10.
Typed
!!! EXPERIMENTAL VERSION !!!
To include autocompletion and further enhance the type hints for these complex constructs the construct_typed package is used as an extension to the original construct package. It is mainly a bunch of Adapters for the original constructs with the focus on type hints.
It implements the following new constructs:
TStruct
,TBitStruct
: similar toconstruct.Struct
but strictly tied toTContainerMixin
and@dataclasses.dataclass
TEnum
: similar toconstruct.Enum
but strictly tied to aTEnumBase
classTFlagsEnum
: similar toconstruct.FlagsEnum
but strictly tied to aTFlagsEnumBase
class
These types are strongly typed, which means that there is no difference between the ParsedType
and the BuildTypes
. So to build one of the constructs the correct type is enforced. The disadvantage is that the code will be a little bit longer, because you can not for example use a normal dict
to build an TStruct
. But the big advantage is, that if you use the correct container type instead of a dict
, the static code analyses can do its magic and find potential type errors and missing values.
A short example:
import dataclasses
import construct as cs
import construct_typed as cst
import typing as t
class Orientation(cst.EnumBase):
HORIZONTAL = 0
VERTICAL = 1
@dataclasses.dataclass
class Image(cst.TContainerMixin):
signature: t.Optional[bytes] = cst.sfield(cs.Const(b"BMP"))
orientation: Orientation = cst.sfield(cst.TEnum(cs.Int8ub, Orientation))
width: int = cst.sfield(cs.Int8ub)
height: int = cst.sfield(cs.Int8ub)
pixels: t.List[int] = cst.sfield(cs.Array(cs.this.width * cs.this.height, cs.Byte))
format = cst.TStruct(Image)
obj = Image(orientation=Orientation.VERTICAL, width=3, height=2, pixels=[7, 8, 9, 11, 12, 13])
print(format.build(obj))
print(format.parse(b"BMP\x01\x03\x02\x07\x08\t\x0b\x0c\r"))
Output:
b'BMP\x01\x03\x02\x07\x08\t\x0b\x0c\r'
Container:
signature = b'BMP' (total 3)
orientation = Orientation.VERTICAL
width = 3
height = 2
pixels = ListContainer:
7
8
9
11
12
13
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for construct_typing-0.4.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69f3a716fc2c9c408cceafb55ea478271b5c8ccf38b64177e63f231f65b207b1 |
|
MD5 | b4e77aaa7ca0e67cc03696872b99b003 |
|
BLAKE2b-256 | 5aa2f7eee1abf0dc35cd8a9b1c3d15bbe8fac323475615f479536a76d0ba1870 |