From 764102a80a9ed6ce5cf6b07d6b74767aeb373317 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Sat, 2 Jan 2021 22:38:18 +0100 Subject: [PATCH] Removed TArray. Instead created TypeAlias "List". --- construct_typed/__init__.py | 5 +++-- construct_typed/helper.py | 9 +++++++++ construct_typed/tarray.py | 40 ------------------------------------- construct_typed/tstruct.py | 4 ++-- tests/test_typed.py | 15 +++++++------- 5 files changed, 21 insertions(+), 52 deletions(-) create mode 100644 construct_typed/helper.py delete mode 100644 construct_typed/tarray.py diff --git a/construct_typed/__init__.py b/construct_typed/__init__.py index 0491326..d5775c1 100644 --- a/construct_typed/__init__.py +++ b/construct_typed/__init__.py @@ -6,10 +6,10 @@ from .generic_wrapper import ( ListContainer, PathType, ) -from .tarray import TArray from .tenum import EnumBase, FlagsEnumBase, TEnum, TFlagsEnum from .tstruct import TBitStruct, TStruct, TStructField, TContainerBase from .tunion import TUnion, TUnionField +from .helper import List, Optional __all__ = [ "TStructField", @@ -18,7 +18,6 @@ __all__ = [ "TEnum", "TUnionField", "TUnion", - "TArray", "EnumBase", "Construct", "Adapter", @@ -29,4 +28,6 @@ __all__ = [ "PathType", "TFlagsEnum", "FlagsEnumBase", + "Optional", + "List" ] diff --git a/construct_typed/helper.py b/construct_typed/helper.py new file mode 100644 index 0000000..8a2b8b0 --- /dev/null +++ b/construct_typed/helper.py @@ -0,0 +1,9 @@ +import typing as t + +from .generic_wrapper import ListContainer + +OptionalType = t.TypeVar("OptionalType") +ListType = t.TypeVar("ListType") + +Optional = t.Optional[OptionalType] +List = t.Union[ListContainer[ListType], t.List[ListType]] \ No newline at end of file diff --git a/construct_typed/tarray.py b/construct_typed/tarray.py deleted file mode 100644 index 06a7e03..0000000 --- a/construct_typed/tarray.py +++ /dev/null @@ -1,40 +0,0 @@ -import typing as t - -import construct as cs - -from .generic_wrapper import * - - -class TArray( - Adapter[ - SubconParsedType, - SubconBuildTypes, - t.List[SubconParsedType], - t.List[SubconParsedType], - ] -): - """ - Adapter for an Array, that transforms the "ListContainer" to an standard "list" while parsing - """ - - def __init__( - self, - count: ConstantOrContextLambda[int], - subcon: Construct[SubconParsedType, SubconBuildTypes], - discard: bool = False, - ) -> None: - # init adatper - super(TArray, self).__init__(cs.Array(count, subcon, discard)) # type: ignore - - def _decode( - self, obj: SubconParsedType, context: Context, path: PathType - ) -> ParsedType: - return list(obj) # type: ignore - - def _encode( - self, - obj: t.List[SubconParsedType], - context: Context, - path: PathType, - ) -> SubconBuildTypes: - return obj # type: ignore diff --git a/construct_typed/tstruct.py b/construct_typed/tstruct.py index 9ff5983..9c8b0ad 100644 --- a/construct_typed/tstruct.py +++ b/construct_typed/tstruct.py @@ -32,14 +32,14 @@ class TContainerBase(_TContainerBase): Note: this always has to be mixed with "dataclasses.dataclass". """ - def __getattribute__(self, name: str): + def __getattribute__(self, name: str) -> t.Any: # if accessing via an field via dot access, return the object from the dict if name in self: return self[name] else: return super().__getattribute__(name) - def __post_init__(self): + def __post_init__(self) -> None: # 1. fix the __keys_order__ of the cs.Container # 2. append fields with init=False to the dict of the cs.Container self.__keys_order__ = [] diff --git a/tests/test_typed.py b/tests/test_typed.py index 783015e..50f80db 100644 --- a/tests/test_typed.py +++ b/tests/test_typed.py @@ -2,7 +2,6 @@ import dataclasses import enum -import typing as t import construct as cs import construct_typed as cst @@ -14,12 +13,12 @@ from .declarativeunittest import common, raises, setattrs def test_tcontainer_compare_with_dataclass() -> None: @dataclasses.dataclass class TestContainer: - a: t.Optional[int] = cst.TStructField(cs.Const(1, cs.Byte)) + a: cst.Optional[int] = cst.TStructField(cs.Const(1, cs.Byte)) b: int = cst.TStructField(cs.Int8ub) @dataclasses.dataclass class TestTContainer(cst.TContainerBase): - a: t.Optional[int] = cst.TStructField(cs.Const(1, cs.Byte)) + a: cst.Optional[int] = cst.TStructField(cs.Const(1, cs.Byte)) b: int = cst.TStructField(cs.Int8ub) datacls = TestContainer(b=1) @@ -67,7 +66,7 @@ def test_tcontainer_compare_with_dataclass() -> None: def test_tcontainer_order() -> None: @dataclasses.dataclass class Image(cst.TContainerBase): - signature: t.Optional[bytes] = cst.TStructField(cs.Const(b"BMP")) + signature: cst.Optional[bytes] = cst.TStructField(cs.Const(b"BMP")) width: int = cst.TStructField(cs.Int8ub) height: int = cst.TStructField(cs.Int8ub) @@ -131,7 +130,7 @@ def test_tstruct_default_field() -> None: class Image(cst.TContainerBase): width: int = cst.TStructField(cs.Int8ub) height: int = cst.TStructField(cs.Int8ub) - pixels: t.Optional[bytes] = cst.TStructField( + pixels: cst.Optional[bytes] = cst.TStructField( cs.Default( cs.Bytes(cs.this.width * cs.this.height), lambda ctx: bytes(ctx.width * ctx.height), @@ -149,7 +148,7 @@ def test_tstruct_default_field() -> None: def test_tstruct_const_field() -> None: @dataclasses.dataclass class TestContainer(cst.TContainerBase): - const_field: t.Optional[bytes] = cst.TStructField(cs.Const(b"\x00")) + const_field: cst.Optional[bytes] = cst.TStructField(cs.Const(b"\x00")) common( cst.TStruct(TestContainer), @@ -170,7 +169,7 @@ def test_tstruct_const_field() -> None: def test_tstruct_anonymus_fields_1() -> None: @dataclasses.dataclass class TestContainer(cst.TContainerBase): - _1: t.Optional[bytes] = cst.TStructField(cs.Const(b"\x00")) + _1: cst.Optional[bytes] = cst.TStructField(cs.Const(b"\x00")) _2: None = cst.TStructField(cs.Padding(1)) _3: None = cst.TStructField(cs.Pass) _4: None = cst.TStructField(cs.Terminated) @@ -187,7 +186,7 @@ def test_tstruct_anonymus_fields_2() -> None: @dataclasses.dataclass class TestContainer(cst.TContainerBase): _1: int = cst.TStructField(cs.Computed(7)) - _2: t.Optional[bytes] = cst.TStructField(cs.Const(b"JPEG")) + _2: cst.Optional[bytes] = cst.TStructField(cs.Const(b"JPEG")) _3: None = cst.TStructField(cs.Pass) _4: None = cst.TStructField(cs.Terminated)