Fixed TStruct in TArray
This commit is contained in:
parent
e6f3f12d30
commit
ede0840a0b
7 changed files with 26 additions and 49 deletions
|
|
@ -168,9 +168,9 @@ class Subconstruct(
|
|||
class Adapter(
|
||||
Subconstruct[SubconParsedType, SubconBuildTypes, ParsedType, BuildTypes],
|
||||
):
|
||||
def __new__(
|
||||
cls, subcon: Construct[SubconParsedType, SubconBuildTypes]
|
||||
) -> Adapter[SubconParsedType, SubconBuildTypes, ParsedType, BuildTypes]: ...
|
||||
def __init__(
|
||||
self, subcon: Construct[SubconParsedType, SubconBuildTypes]
|
||||
) -> None: ...
|
||||
def _decode(
|
||||
self, obj: SubconParsedType, context: Context, path: PathType
|
||||
) -> ParsedType: ...
|
||||
|
|
@ -503,7 +503,7 @@ class Default(Subconstruct[SubconParsedType, SubconBuildTypes, ParsedType, Build
|
|||
def __new__(
|
||||
cls,
|
||||
subcon: Construct[SubconParsedType, SubconBuildTypes],
|
||||
value: SubconBuildTypes,
|
||||
value: ConstantOrContextLambda2[SubconBuildTypes],
|
||||
) -> Default[
|
||||
SubconParsedType,
|
||||
SubconBuildTypes,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from .tarray import *
|
||||
from .tenum import *
|
||||
from .tstruct import *
|
||||
from .tunion import *
|
||||
from .tarray import TArray
|
||||
from .tenum import TEnum
|
||||
from .tstruct import TStruct, TBitStruct, StructField
|
||||
from .tunion import TUnion, UnionField
|
||||
|
||||
__all__ = [
|
||||
"StructField",
|
||||
|
|
|
|||
|
|
@ -8,10 +8,8 @@ ParsedType = t.TypeVar("ParsedType")
|
|||
BuildTypes = t.TypeVar("BuildTypes")
|
||||
SubconParsedType = t.TypeVar("SubconParsedType")
|
||||
SubconBuildTypes = t.TypeVar("SubconBuildTypes")
|
||||
DataclassType = t.TypeVar("DataclassType")
|
||||
ListType = t.TypeVar("ListType")
|
||||
ValueType = t.TypeVar("ValueType")
|
||||
EnumType = t.TypeVar("EnumType", bound=enum.IntEnum)
|
||||
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
|
|
|
|||
|
|
@ -5,31 +5,20 @@ from .generic_wrapper import *
|
|||
|
||||
class TArray(
|
||||
Adapter[
|
||||
t.Any,
|
||||
t.Any,
|
||||
ParsedType,
|
||||
BuildTypes,
|
||||
SubconParsedType,
|
||||
SubconBuildTypes,
|
||||
t.List[SubconParsedType],
|
||||
t.List[SubconParsedType],
|
||||
]
|
||||
):
|
||||
"""
|
||||
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],
|
||||
subcon: Construct[SubconParsedType, SubconBuildTypes],
|
||||
discard: bool = False,
|
||||
) -> None:
|
||||
# init adatper
|
||||
|
|
@ -42,8 +31,8 @@ class TArray(
|
|||
|
||||
def _encode(
|
||||
self,
|
||||
obj: BuildTypes,
|
||||
obj: t.Any,
|
||||
context: Context,
|
||||
path: PathType,
|
||||
) -> t.List[BuildTypes]:
|
||||
) -> t.List[t.Any]:
|
||||
return obj # type: ignore
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ import typing as t
|
|||
import construct as cs
|
||||
from .generic_wrapper import *
|
||||
|
||||
EnumType = t.TypeVar("EnumType", bound=enum.IntEnum)
|
||||
|
||||
|
||||
class TEnum(Adapter[int, int, EnumType, t.Union[int, str, EnumType]]):
|
||||
def __new__(
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ import textwrap
|
|||
import dataclasses
|
||||
from .generic_wrapper import *
|
||||
|
||||
DataclassType = t.TypeVar("DataclassType")
|
||||
|
||||
|
||||
def StructField(
|
||||
subcon: Construct[ParsedType, BuildTypes],
|
||||
|
|
@ -44,7 +46,7 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]):
|
|||
"""
|
||||
|
||||
def __init__(
|
||||
self, dataclass_type: t.Type[DataclassType], swapped: bool = False
|
||||
self, dataclass_type: t.Type[ParsedType], swapped: bool = False
|
||||
) -> None:
|
||||
if not dataclasses.is_dataclass(dataclass_type):
|
||||
raise TypeError(
|
||||
|
|
@ -75,7 +77,7 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]):
|
|||
|
||||
def _decode(
|
||||
self, obj: "cs.Container[t.Any]", context: "cs.Context", path: "cs.PathType"
|
||||
) -> DataclassType:
|
||||
) -> ParsedType:
|
||||
# get all fields from the dataclass
|
||||
fields = dataclasses.fields(self.dataclass_type)
|
||||
|
||||
|
|
@ -98,7 +100,7 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]):
|
|||
return dc
|
||||
|
||||
def _encode(
|
||||
self, obj: DataclassType, context: "cs.Context", path: "cs.PathType"
|
||||
self, obj: ParsedType, context: "cs.Context", path: "cs.PathType"
|
||||
) -> t.Dict[str, t.Any]:
|
||||
# get all fields from the dataclass
|
||||
fields = dataclasses.fields(self.dataclass_type)
|
||||
|
|
@ -112,38 +114,22 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]):
|
|||
return ret_dict
|
||||
|
||||
|
||||
class TStruct(_TStruct[ParsedType, BuildTypes]):
|
||||
class TStruct(_TStruct[ParsedType, ParsedType]):
|
||||
"""
|
||||
Typed struct, based on standard dataclasses.
|
||||
"""
|
||||
|
||||
# this is unfortunately needed because the stubs are using __new__ instead of __init__
|
||||
if t.TYPE_CHECKING:
|
||||
|
||||
def __new__(
|
||||
cls, dataclass_type: t.Type[DataclassType], swapped: bool = False
|
||||
) -> "TStruct[DataclassType, DataclassType]":
|
||||
...
|
||||
|
||||
def _create_subcon(
|
||||
self, subcon_fields: t.Dict[str, t.Any]
|
||||
) -> Construct[t.Any, t.Any]:
|
||||
return cs.Struct(**subcon_fields)
|
||||
|
||||
|
||||
class TBitStruct(_TStruct[ParsedType, BuildTypes]):
|
||||
class TBitStruct(_TStruct[ParsedType, ParsedType]):
|
||||
"""
|
||||
Typed bit struct, based on standard dataclasses.
|
||||
"""
|
||||
|
||||
# this is unfortunately needed because the stubs are using __new__ instead of __init__
|
||||
if t.TYPE_CHECKING:
|
||||
|
||||
def __new__(
|
||||
cls, dataclass_type: t.Type[DataclassType], swapped: bool = False
|
||||
) -> "TBitStruct[DataclassType, DataclassType]":
|
||||
...
|
||||
|
||||
def _create_subcon(
|
||||
self, subcon_fields: t.Dict[str, t.Any]
|
||||
) -> Construct[t.Any, t.Any]:
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import construct as cs
|
|||
import dataclasses
|
||||
from .generic_wrapper import *
|
||||
|
||||
DataclassType = t.TypeVar("DataclassType")
|
||||
|
||||
|
||||
def UnionField(
|
||||
subcon: Construct[ParsedType, BuildTypes],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue