construct-typing


Nameconstruct-typing JSON
Version 0.6.2 PyPI version JSON
download
home_pagehttps://github.com/timrid/construct-typing
SummaryExtension for the python package 'construct' that adds typing features
upload_time2023-08-03 07:31:06
maintainer
docs_urlNone
authorTim Riddermann
requires_python>=3.7
licenseMIT
keywords construct kaitai declarative data structure struct binary symmetric parser builder parsing building pack unpack packer unpacker bitstring bytestring annotation type hint typing typed bitstruct pep 561
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # construct-typing
[![PyPI](https://img.shields.io/pypi/v/construct-typing)](https://pypi.org/project/construct-typing/)
![PyPI - Implementation](https://img.shields.io/pypi/implementation/construct-typing)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/construct-typing)
![GitHub](https://img.shields.io/github/license/timrid/construct-typing)

This project is an extension of the python package [*construct*](https://pypi.org/project/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](https://www.python.org/dev/peps/pep-0561/#stub-only-packages))
- **construct_typed**: Adding additional classes that help with autocompletion and additional type hints.

## Installation
This package comply to [PEP 561](https://www.python.org/dev/peps/pep-0561/). 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
- pyright

## 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 by `BuildTypes` to a `bytes` object.
- `Construct.parse`: converts a `bytes` object to an object of type `ParsedType`.

For each `Construct` the stub file defines to 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]`        | `typing.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 an `Array` 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](https://mail.python.org/archives/list/typing-sig@python.org/thread/SQVTQYWIOI4TIO7NNBTFFWFMSMS2TA4J/) 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 few Adapters with the focus on type hints.

It implements the following new constructs:
- `DataclassStruct`: similar to `construct.Struct` but strictly tied to `DataclassMixin` and `@dataclasses.dataclass`
- `DataclassBitStruct`: similar to `construct.BitStruct` but strictly tied to `DataclassMixin` and `@dataclasses.dataclass`
- `TEnum`: similar to `construct.Enum` but strictly tied to a `TEnumBase` class
- `TFlagsEnum`: similar to `construct.FlagsEnum` but strictly tied to a `TFlagsEnumBase` 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 `DataclassStruct`. 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 without running the code itself.


A short example:

```python
import dataclasses
import typing as t
from construct import Array, Byte, Const, Int8ub, this
from construct_typed import DataclassMixin, DataclassStruct, EnumBase, TEnum, csfield

class Orientation(EnumBase):
    HORIZONTAL = 0
    VERTICAL = 1

@dataclasses.dataclass
class Image(DataclassMixin):
    signature: bytes = csfield(Const(b"BMP"))
    orientation: Orientation = csfield(TEnum(Int8ub, Orientation))
    width: int = csfield(Int8ub)
    height: int = csfield(Int8ub)
    pixels: t.List[int] = csfield(Array(this.width * this.height, Byte))

format = DataclassStruct(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'
Image: 
    signature = b'BMP' (total 3)
    orientation = Orientation.VERTICAL
    width = 3
    height = 2
    pixels = ListContainer:
        7
        8
        9
        11
        12
        13
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/timrid/construct-typing",
    "name": "construct-typing",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "construct,kaitai,declarative,data structure,struct,binary,symmetric,parser,builder,parsing,building,pack,unpack,packer,unpacker,bitstring,bytestring,annotation,type hint,typing,typed,bitstruct,PEP 561",
    "author": "Tim Riddermann",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/f1/13/c609e60a687252813aa4b69f989f42754ccd5e217717216fc852eefedfd7/construct-typing-0.6.2.tar.gz",
    "platform": "POSIX",
    "description": "# construct-typing\n[![PyPI](https://img.shields.io/pypi/v/construct-typing)](https://pypi.org/project/construct-typing/)\n![PyPI - Implementation](https://img.shields.io/pypi/implementation/construct-typing)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/construct-typing)\n![GitHub](https://img.shields.io/github/license/timrid/construct-typing)\n\nThis project is an extension of the python package [*construct*](https://pypi.org/project/construct/), which is a powerful **declarative** and **symmetrical** parser and builder for binary data. This Repository consists of two packages:\n\n- **construct-stubs**: Adding .pyi for the whole *construct 2.10* package (according to  [PEP 561 stub-only packages](https://www.python.org/dev/peps/pep-0561/#stub-only-packages))\n- **construct_typed**: Adding additional classes that help with autocompletion and additional type hints.\n\n## Installation\nThis package comply to [PEP 561](https://www.python.org/dev/peps/pep-0561/). So most of the static code analysers will recognise the stubs automatically. The installation only requires:\n```\npip install construct-typing\n```\n\n## Tests\nThe 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.\n\nThe new typed constructs have new written pytests, which also passes all pytests and the static type checkers.\n\nThe following static type checkers are fully supported:\n- mypy\n- pyright\n\n## Explanation\n### Stubs\nThe **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`:\n- `Construct.build`: converts an object of one of the types defined by `BuildTypes` to a `bytes` object.\n- `Construct.parse`: converts a `bytes` object to an object of type `ParsedType`.\n\nFor each `Construct` the stub file defines to which type it parses to and from which it can be build. For example:\n\n| Construct            | parses to (ParsedType)         | builds from (BuildTypes)                 |\n| -------------------- | ------------------------------ | ---------------------------------------- |\n| `Int16ub`            | `int`                          | `int`                                    |\n| `Bytes`              | `bytes`                        | `bytes`, `bytearray` or `memoryview`     |\n| `Array(5, Int16ub)`  | `ListContainer[int]`           | `typing.List[int]`                       |\n| `Struct(\"i\" / Byte)` | `Container[typing.Any]`        | `typing.Dict[str, typing.Any]` or `None` |\n\nThe problem is to describe the more complex constructs like:\n - `Sequence`, `FocusedSeq` which has heterogenous subcons in comparison to an `Array` with only homogenous subcons. \n - `Struct`, `BitStruct`, `LazyStruct`, `Union` which has heterogenous and named subcons.\n\nCurrently 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](https://mail.python.org/archives/list/typing-sig@python.org/thread/SQVTQYWIOI4TIO7NNBTFFWFMSMS2TA4J/) become available). But the biggest disadvantage is that autocompletion for the named subcons is not available.\n\nNote: The stubs are based on *construct* in Version 2.10.\n\n\n### Typed\n**!!! EXPERIMENTAL VERSION !!!**\n\nTo 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 few Adapters with the focus on type hints.\n\nIt implements the following new constructs:\n- `DataclassStruct`: similar to `construct.Struct` but strictly tied to `DataclassMixin` and `@dataclasses.dataclass`\n- `DataclassBitStruct`: similar to `construct.BitStruct` but strictly tied to `DataclassMixin` and `@dataclasses.dataclass`\n- `TEnum`: similar to `construct.Enum` but strictly tied to a `TEnumBase` class\n- `TFlagsEnum`: similar to `construct.FlagsEnum` but strictly tied to a `TFlagsEnumBase` class\n\nThese 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 `DataclassStruct`. 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 without running the code itself.\n\n\nA short example:\n\n```python\nimport dataclasses\nimport typing as t\nfrom construct import Array, Byte, Const, Int8ub, this\nfrom construct_typed import DataclassMixin, DataclassStruct, EnumBase, TEnum, csfield\n\nclass Orientation(EnumBase):\n    HORIZONTAL = 0\n    VERTICAL = 1\n\n@dataclasses.dataclass\nclass Image(DataclassMixin):\n    signature: bytes = csfield(Const(b\"BMP\"))\n    orientation: Orientation = csfield(TEnum(Int8ub, Orientation))\n    width: int = csfield(Int8ub)\n    height: int = csfield(Int8ub)\n    pixels: t.List[int] = csfield(Array(this.width * this.height, Byte))\n\nformat = DataclassStruct(Image)\nobj = Image(\n    orientation=Orientation.VERTICAL,\n    width=3,\n    height=2,\n    pixels=[7, 8, 9, 11, 12, 13],\n)\nprint(format.build(obj))\nprint(format.parse(b\"BMP\\x01\\x03\\x02\\x07\\x08\\t\\x0b\\x0c\\r\"))\n```\nOutput:\n```\nb'BMP\\x01\\x03\\x02\\x07\\x08\\t\\x0b\\x0c\\r'\nImage: \n    signature = b'BMP' (total 3)\n    orientation = Orientation.VERTICAL\n    width = 3\n    height = 2\n    pixels = ListContainer:\n        7\n        8\n        9\n        11\n        12\n        13\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Extension for the python package 'construct' that adds typing features",
    "version": "0.6.2",
    "project_urls": {
        "Homepage": "https://github.com/timrid/construct-typing"
    },
    "split_keywords": [
        "construct",
        "kaitai",
        "declarative",
        "data structure",
        "struct",
        "binary",
        "symmetric",
        "parser",
        "builder",
        "parsing",
        "building",
        "pack",
        "unpack",
        "packer",
        "unpacker",
        "bitstring",
        "bytestring",
        "annotation",
        "type hint",
        "typing",
        "typed",
        "bitstruct",
        "pep 561"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b20bab3ce2b27dd74b6a6703065bd304ea8211ff4de3b1c304446ed95234177b",
                "md5": "44be926ba3dad8017ef9d0d3cef9006e",
                "sha256": "ebea6989ac622d0c4eb457092cef0c7bfbcfa110bd018670fea7064d0bc09e47"
            },
            "downloads": -1,
            "filename": "construct_typing-0.6.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "44be926ba3dad8017ef9d0d3cef9006e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 23298,
            "upload_time": "2023-08-03T07:31:04",
            "upload_time_iso_8601": "2023-08-03T07:31:04.545420Z",
            "url": "https://files.pythonhosted.org/packages/b2/0b/ab3ce2b27dd74b6a6703065bd304ea8211ff4de3b1c304446ed95234177b/construct_typing-0.6.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f113c609e60a687252813aa4b69f989f42754ccd5e217717216fc852eefedfd7",
                "md5": "0304eba4a6056ff8220e2f907ed0b3c3",
                "sha256": "948e998cfc003681dc34f2d071c3a688cf35b805cbe107febbc488ef967ccba1"
            },
            "downloads": -1,
            "filename": "construct-typing-0.6.2.tar.gz",
            "has_sig": false,
            "md5_digest": "0304eba4a6056ff8220e2f907ed0b3c3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 22029,
            "upload_time": "2023-08-03T07:31:06",
            "upload_time_iso_8601": "2023-08-03T07:31:06.205459Z",
            "url": "https://files.pythonhosted.org/packages/f1/13/c609e60a687252813aa4b69f989f42754ccd5e217717216fc852eefedfd7/construct-typing-0.6.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-03 07:31:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "timrid",
    "github_project": "construct-typing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "construct-typing"
}
        
Elapsed time: 0.11178s