construct-typing/construct_typed/tarray.py
Tim Rid e6f3f12d30 - divided construct_typed in multiple files
- adapted the "test_typed.py" to dataclasses
2020-12-31 17:11:21 +01:00

49 lines
1.3 KiB
Python

import typing as t
import construct as cs
from .generic_wrapper import *
class TArray(
Adapter[
t.Any,
t.Any,
ParsedType,
BuildTypes,
]
):
"""
Adapter for an Array, that transforms the "ListContainer" to an standard "list" while parsing
"""
# this is unfortunately needed because the stubs are using __new__ instead of __init__
if t.TYPE_CHECKING:
def __new__(
cls,
count: ConstantOrContextLambda[int],
subcon: Construct[SubconParsedType, SubconBuildTypes],
discard: bool = False,
) -> "TArray[t.List[SubconParsedType], t.List[SubconParsedType]]":
...
def __init__(
self,
count: ConstantOrContextLambda[int],
subcon: Construct[ParsedType, BuildTypes],
discard: bool = False,
) -> None:
# init adatper
super(TArray, self).__init__(cs.Array(count, subcon, discard)) # type: ignore
def _decode(
self, obj: ListContainer[ParsedType], context: Context, path: PathType
) -> ParsedType:
return list(obj) # type: ignore
def _encode(
self,
obj: BuildTypes,
context: Context,
path: PathType,
) -> t.List[BuildTypes]:
return obj # type: ignore