Removed TArray. Instead created TypeAlias "List".

This commit is contained in:
Tim Rid 2021-01-02 22:38:18 +01:00
parent 8ed38ddb5a
commit 764102a80a
5 changed files with 21 additions and 52 deletions

View file

@ -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"
]

View file

@ -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]]

View file

@ -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

View file

@ -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__ = []

View file

@ -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)