some renaming

This commit is contained in:
Tim Rid 2022-02-13 19:30:35 +01:00
parent 8525c04165
commit e99dd5d752
2 changed files with 20 additions and 20 deletions

View file

@ -162,15 +162,15 @@ class TFlags(enum.IntFlag, metaclass=_EnumMeta):
@classmethod
def __construct__(
cls: "t.Type[FlagsEnumType]",
) -> "TFlagsConstruct[FlagsEnumType]":
cls: "t.Type[FlagsType]",
) -> "TFlagsConstruct[FlagsType]":
...
FlagsEnumType = t.TypeVar("FlagsEnumType", bound=TFlags)
FlagsType = t.TypeVar("FlagsType", bound=TFlags)
class TFlagsConstruct(Adapter[int, int, FlagsEnumType, FlagsEnumType]):
class TFlagsConstruct(Adapter[int, int, FlagsType, FlagsType]):
"""
Typed flags.
"""
@ -178,28 +178,28 @@ class TFlagsConstruct(Adapter[int, int, FlagsEnumType, FlagsEnumType]):
if t.TYPE_CHECKING:
def __new__(
cls, subcon: Construct[int, int], enum_type: t.Type[FlagsEnumType]
) -> "TFlagsConstruct[FlagsEnumType]":
cls, subcon: Construct[int, int], enum_type: t.Type[FlagsType]
) -> "TFlagsConstruct[FlagsType]":
...
def __init__(self, subcon: Construct[int, int], enum_type: t.Type[FlagsEnumType]):
def __init__(self, subcon: Construct[int, int], enum_type: t.Type[FlagsType]):
if not issubclass(enum_type, TFlags):
raise TypeError(
"'{}' has to be a '{}'".format(repr(enum_type), repr(TFlags))
)
# save enum type
self.enum_type = t.cast(t.Type[FlagsEnumType], enum_type) # type: ignore
self.enum_type = t.cast(t.Type[FlagsType], enum_type) # type: ignore
# init adatper
super(TFlagsConstruct, self).__init__(subcon) # type: ignore
def _decode(self, obj: int, context: Context, path: PathType) -> FlagsEnumType:
def _decode(self, obj: int, context: Context, path: PathType) -> FlagsType:
return self.enum_type(obj)
def _encode(
self,
obj: FlagsEnumType,
obj: FlagsType,
context: Context,
path: PathType,
) -> int:

View file

@ -359,19 +359,19 @@ def test_tenum_in_dataclass_struct() -> None:
)
def test_tenum_flags() -> None:
class TestEnum(TFlags, subcon=cs.Byte):
def test_tflags() -> None:
class TestFlags(TFlags, subcon=cs.Byte):
one = 1
two = 2
four = 4
eight = 8
d = construct(TestEnum)
common(d, b"\x03", TestEnum.one | TestEnum.two, 1)
assert d.build(TestEnum(0)) == b"\x00"
assert d.build(TestEnum.one | TestEnum.two) == b"\x03"
assert d.build(TestEnum(8)) == b"\x08"
assert d.build(TestEnum(1 | 2)) == b"\x03"
assert d.build(TestEnum(255)) == b"\xff"
assert d.build(TestEnum.eight) == b"\x08"
d = construct(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"
assert d.build(TestFlags(8)) == b"\x08"
assert d.build(TestFlags(1 | 2)) == b"\x03"
assert d.build(TestFlags(255)) == b"\xff"
assert d.build(TestFlags.eight) == b"\x08"
assert raises(d.build, 2) == TypeError