diff --git a/construct_typed/__init__.py b/construct_typed/__init__.py index 1c11365..56c0e57 100644 --- a/construct_typed/__init__.py +++ b/construct_typed/__init__.py @@ -1,4 +1,4 @@ -from construct_typed.generic import construct +from construct_typed.generic import constr from construct_typed.dataclass_struct import ( DataclassBitStruct, DataclassStruct, @@ -18,7 +18,7 @@ from construct_typed.tenum import TEnum, TFlags, TEnumConstruct, TFlagsConstruct __all__ = [ "DataclassBitStruct", "DataclassStruct", - "construct", + "constr", "csfield", "TEnum", "TEnumConstruct", diff --git a/construct_typed/dataclass_struct.py b/construct_typed/dataclass_struct.py index f9f9705..d6a8566 100644 --- a/construct_typed/dataclass_struct.py +++ b/construct_typed/dataclass_struct.py @@ -78,7 +78,7 @@ def csfield( init = True default = dataclasses.MISSING - # Set default values in case of special sucons + # Set default values in case of special subcons if isinstance(orig_subcon, cs.Const): const_subcon: "cs.Const[t.Any, t.Any, t.Any, t.Any]" = orig_subcon default = const_subcon.value @@ -249,7 +249,7 @@ class DataclassStruct: _replace_this_struct(constr, dc_constr) # save construct format and make the class compatible to `Constructable` protocol - setattr(cls, "__construct__", lambda: constr) + setattr(cls, "__constr__", lambda: constr) return cls @@ -303,7 +303,7 @@ class DataclassStruct: if t.TYPE_CHECKING: @classmethod - def __construct__(cls: t.Type[T]) -> "DataclassConstruct[T]": + def __constr__(cls: t.Type[T]) -> "DataclassConstruct[T]": ... diff --git a/construct_typed/generic.py b/construct_typed/generic.py index 75c0ac2..b988444 100644 --- a/construct_typed/generic.py +++ b/construct_typed/generic.py @@ -43,16 +43,16 @@ else: @t.runtime_checkable class Constructable(t.Protocol[ParsedType, BuildTypes]): - def __construct__(self) -> "Construct[ParsedType, BuildTypes]": + def __constr__(self) -> "Construct[ParsedType, BuildTypes]": raise NotImplementedError -def construct( +def constr( constr: t.Union[ Constructable[ParsedType, BuildTypes], "Construct[ParsedType, BuildTypes]" ], ) -> Construct[ParsedType, BuildTypes]: """Get construct instance of `Constructable` or `Construct`""" if isinstance(constr, Constructable): - constr = constr.__construct__() + constr = constr.__constr__() return constr diff --git a/construct_typed/tenum.py b/construct_typed/tenum.py index b146549..2cf5316 100644 --- a/construct_typed/tenum.py +++ b/construct_typed/tenum.py @@ -52,7 +52,7 @@ class _EnumMeta(enum.EnumMeta): enum_constr = None # save construct format and make the class compatible to `Constructable` protocol - setattr(cls, "__construct__", lambda: enum_constr) # type: ignore + setattr(cls, "__constr__", lambda: enum_constr) # type: ignore return cls @@ -78,7 +78,7 @@ class TEnum(enum.IntEnum, metaclass=_EnumMeta): ... @classmethod - def __construct__(cls: "t.Type[EnumType]") -> "TEnumConstruct[EnumType]": + def __constr__(cls: "t.Type[EnumType]") -> "TEnumConstruct[EnumType]": ... # Extend the enum type with __missing__ method. So if a enum value @@ -161,7 +161,7 @@ class TFlags(enum.IntFlag, metaclass=_EnumMeta): ... @classmethod - def __construct__( + def __constr__( cls: "t.Type[FlagsType]", ) -> "TFlagsConstruct[FlagsType]": ... diff --git a/tests/test_typed.py b/tests/test_typed.py index 8efdbb5..ef0226b 100644 --- a/tests/test_typed.py +++ b/tests/test_typed.py @@ -7,7 +7,7 @@ from construct_typed import ( DataclassBitStruct, DataclassStruct, csfield, - construct, + constr, TEnum, TFlags, ) @@ -61,7 +61,7 @@ def test_dataclass_str_repr() -> None: width: int = csfield(cs.Int8ub) height: int = csfield(cs.Int8ub) - format = construct(Image) + format = constr(Image) obj = Image(width=3, height=2) assert ( str(obj) @@ -81,13 +81,13 @@ def test_dataclass_struct() -> None: pixels: bytes = csfield(cs.Bytes(cs.this.height * cs.this.width)) common( - construct(Image), + constr(Image), b"\x01\x0212", Image(width=1, height=2, pixels=b"12"), ) # check __getattr__ - c = Image.__construct__() + c = Image.__constr__() assert c.width.name == "width" assert c.height.name == "height" assert c.width.subcon is cs.Int8ub @@ -100,7 +100,7 @@ def test_dataclass_struct_reverse() -> None: b: int = csfield(cs.Int8ub) common( - construct(TestContainer), + constr(TestContainer), b"\x02\x00\x01", TestContainer(a=1, b=2), 3, @@ -114,10 +114,10 @@ def test_dataclass_struct_nested() -> None: c: bytes = csfield(cs.Bytes(cs.this._.length)) length: int = csfield(cs.Byte) - a: InnerDataclass = csfield(construct(InnerDataclass)) + a: InnerDataclass = csfield(constr(InnerDataclass)) common( - construct(TestContainer), + constr(TestContainer), b"\x02\x01\xF1\xF2", TestContainer(length=2, a=TestContainer.InnerDataclass(b=1, c=b"\xF1\xF2")), ) @@ -135,7 +135,7 @@ def test_dataclass_struct_default_field() -> None: ) common( - construct(Image), + constr(Image), b"\x02\x03\x00\x00\x00\x00\x00\x00", setattrs(Image(2, 3), pixels=bytes(6)), sample_building=Image(2, 3), @@ -147,7 +147,7 @@ def test_dataclass_struct_const_field() -> None: const_field: t.Optional[bytes] = csfield(cs.Const(b"\x00")) common( - construct(TestContainer), + constr(TestContainer), bytes(1), setattrs(TestContainer(), const_field=b"\x00"), 1, @@ -155,7 +155,7 @@ def test_dataclass_struct_const_field() -> None: assert ( raises( - construct(TestContainer).build, + constr(TestContainer).build, setattrs(TestContainer(), const_field=b"\x01"), ) == cs.ConstError @@ -167,7 +167,7 @@ def test_dataclass_struct_array_field() -> None: array_field: t.List[int] = csfield(cs.Array(5, cs.Int8ub)) common( - construct(TestContainer), + constr(TestContainer), bytes(5), TestContainer(array_field=[0, 0, 0, 0, 0]), 5, @@ -182,7 +182,7 @@ def test_dataclass_struct_anonymus_fields_1() -> None: _4: None = csfield(cs.Terminated) common( - construct(TestContainer), + constr(TestContainer), bytes(2), setattrs(TestContainer(), _1=b"\x00"), cs.SizeofError, @@ -196,7 +196,7 @@ def test_dataclass_struct_anonymus_fields_2() -> None: _3: None = csfield(cs.Pass) _4: None = csfield(cs.Terminated) - d = construct(TestContainer) + d = constr(TestContainer) assert d.build(TestContainer()) == d.build(TestContainer()) @@ -219,7 +219,7 @@ def test_dataclass_struct_overloaded_method() -> None: update: int = csfield(cs.Int8ul) values: int = csfield(cs.Int8ul) - d = construct(TestContainer) + d = constr(TestContainer) obj = d.parse( d.build( TestContainer( @@ -266,7 +266,7 @@ def test_dataclass_struct_wrong_container() -> None: b: int = csfield(cs.Int8ub) assert ( - raises(construct(TestContainer1).build, TestContainer2(a=1, b=2)) == TypeError + raises(constr(TestContainer1).build, TestContainer2(a=1, b=2)) == TypeError ) @@ -284,7 +284,7 @@ def test_dataclass_struct_doc() -> None: """, ) - format = TestContainer.__construct__() + format = TestContainer.__constr__() common(format, b"\x00\x01\x02\x03", TestContainer(a=1, b=2, c=3), 4) assert format.subcon.a.docs == "This is the documentation of a" @@ -302,14 +302,14 @@ def test_dataclass_bitstruct() -> None: c: int = csfield(cs.BitsInteger(8)) common( - construct(TestContainer), + constr(TestContainer), b"\xFD\x12", TestContainer(a=0x7E, b=1, c=0x12), 2, ) # check __getattr__ - c = TestContainer.__construct__() + c = TestContainer.__constr__() assert c.subcon.a.name == "a" assert c.subcon.b.name == "b" assert c.subcon.c.name == "c" @@ -325,7 +325,7 @@ def test_tenum() -> None: four = 4 eight = 8 - d = construct(TestEnum) + d = constr(TestEnum) common(d, b"\x01", TestEnum.one, 1) common(d, b"\xff", TestEnum(255), 1) @@ -344,18 +344,18 @@ def test_tenum_in_dataclass_struct() -> None: b = 2 class TestContainer(DataclassStruct): - a: TestEnum = csfield(construct(TestEnum)) + a: TestEnum = csfield(constr(TestEnum)) b: int = csfield(cs.Int8ub) common( - construct(TestContainer), + constr(TestContainer), b"\x01\x02", TestContainer(a=TestEnum.a, b=2), 2, ) assert ( - raises(construct(TestEnum).build, TestContainer(a=1, b=2)) == TypeError # type: ignore + raises(constr(TestEnum).build, TestContainer(a=1, b=2)) == TypeError # type: ignore ) @@ -366,7 +366,7 @@ def test_tflags() -> None: four = 4 eight = 8 - d = construct(TestFlags) + d = constr(TestFlags) common(d, b"\x03", TestFlags.one | TestFlags.two, 1) assert d.build(TestFlags(0)) == b"\x00" assert d.build(TestFlags.one | TestFlags.two) == b"\x03"