fixed some mypy issues
This commit is contained in:
parent
be5ae240fb
commit
75dbd8d822
3 changed files with 39 additions and 40 deletions
|
|
@ -31,29 +31,29 @@ def __dataclass_transform__(
|
|||
|
||||
DATACLASS_METADATA_KEY = "__construct_typed_subcon"
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
# specialisation for constructs, that builds from none and dont have to be declared in the __init__ method
|
||||
@t.overload
|
||||
def csfield(
|
||||
subcon: cs.Construct[ParsedType, None],
|
||||
doc: t.Optional[str] = None,
|
||||
parsed: t.Optional[t.Callable[[t.Any, Context], None]] = None,
|
||||
init: t.Literal[False] = False,
|
||||
) -> ParsedType:
|
||||
...
|
||||
# specialisation for constructs, that builds from none and dont have to be declared in the __init__ method
|
||||
@t.overload
|
||||
def csfield(
|
||||
subcon: "cs.Construct[ParsedType, None]",
|
||||
doc: t.Optional[str] = None,
|
||||
parsed: t.Optional[t.Callable[[t.Any, Context], None]] = None,
|
||||
init: t.Literal[False] = False,
|
||||
) -> ParsedType:
|
||||
...
|
||||
|
||||
@t.overload
|
||||
def csfield(
|
||||
subcon: Construct[ParsedType, t.Any],
|
||||
doc: t.Optional[str] = None,
|
||||
parsed: t.Optional[t.Callable[[t.Any, Context], None]] = None,
|
||||
init: bool = True,
|
||||
) -> ParsedType:
|
||||
...
|
||||
|
||||
@t.overload
|
||||
def csfield(
|
||||
subcon: "Construct[ParsedType, t.Any]",
|
||||
doc: t.Optional[str] = None,
|
||||
parsed: t.Optional[t.Callable[[t.Any, Context], None]] = None,
|
||||
init: bool = True,
|
||||
) -> ParsedType:
|
||||
...
|
||||
|
||||
|
||||
def csfield(
|
||||
subcon: Construct[ParsedType, t.Any],
|
||||
subcon: "Construct[ParsedType, t.Any]",
|
||||
doc: t.Optional[str] = None,
|
||||
parsed: t.Optional[t.Callable[[t.Any, Context], None]] = None,
|
||||
init: bool = True,
|
||||
|
|
@ -119,7 +119,9 @@ class DataclassConstruct(Adapter[t.Any, t.Any, T, T]):
|
|||
reverse: bool = False,
|
||||
) -> None:
|
||||
if not issubclass(dc_type, DataclassStruct):
|
||||
raise TypeError(f"'{repr(dc_type)}' has to be a subclass of 'DataclassStruct'")
|
||||
raise TypeError(
|
||||
f"'{repr(dc_type)}' has to be a subclass of 'DataclassStruct'"
|
||||
)
|
||||
if not dataclasses.is_dataclass(dc_type):
|
||||
raise TypeError(f"'{repr(dc_type)}' has to be a 'dataclasses.dataclass'")
|
||||
self.dc_type = dc_type
|
||||
|
|
@ -155,7 +157,7 @@ class DataclassConstruct(Adapter[t.Any, t.Any, T, T]):
|
|||
dc_init[field.name] = value
|
||||
|
||||
# create object of dataclass
|
||||
dc = self.dc_type(**dc_init) # type: ignore
|
||||
dc: T = self.dc_type(**dc_init) # type: ignore
|
||||
|
||||
# extract all other values from the container, an pass it to the dataclass
|
||||
for field in fields:
|
||||
|
|
@ -185,7 +187,7 @@ class DataclassConstruct(Adapter[t.Any, t.Any, T, T]):
|
|||
this_struct: Construct[t.Any, t.Any] = Construct()
|
||||
|
||||
|
||||
def _replace_this_struct(constr: "Construct[t.Any, t.Any]", replacement: t.Any):
|
||||
def _replace_this_struct(constr: "Construct[t.Any, t.Any]", replacement: t.Any) -> None:
|
||||
"""Recursive search for `this_struct` in all SubConstructs and replace it with AttrsStruct"""
|
||||
subcon = getattr(constr, "subcon", None)
|
||||
if subcon is this_struct:
|
||||
|
|
@ -231,7 +233,7 @@ class DataclassStruct:
|
|||
cls,
|
||||
constr: "cs.Construct[t.Any, t.Any]" = this_struct,
|
||||
reverse_fields: bool = False,
|
||||
):
|
||||
) -> None:
|
||||
# validate types
|
||||
if not isinstance(constr, cs.Construct): # type: ignore
|
||||
raise ValueError("`constr` parameter has to be an `Construct` object")
|
||||
|
|
@ -251,8 +253,6 @@ class DataclassStruct:
|
|||
# save construct format and make the class compatible to `Constructable` protocol
|
||||
setattr(cls, "__constr__", lambda: constr)
|
||||
|
||||
return cls
|
||||
|
||||
# the `construct` library is using the [] access internally, so struct objects
|
||||
# should also make this possible and not only via the dot access.
|
||||
def __getitem__(self, key: str) -> t.Any:
|
||||
|
|
@ -336,6 +336,5 @@ class DataclassBitStruct(DataclassStruct):
|
|||
cls,
|
||||
constr: "cs.Construct[t.Any, t.Any]" = this_struct,
|
||||
reverse_fields: bool = False,
|
||||
):
|
||||
) -> None:
|
||||
cls = DataclassStruct.__init_subclass__.__func__(cls, cs.Bitwise(constr), reverse_fields) # type: ignore
|
||||
return cls
|
||||
|
|
|
|||
|
|
@ -12,22 +12,22 @@ class _EnumMeta(enum.EnumMeta):
|
|||
@classmethod
|
||||
def __prepare__(
|
||||
metacls, # type: ignore
|
||||
name: str,
|
||||
bases: t.Tuple[type, ...],
|
||||
__name: str,
|
||||
__bases: t.Tuple[type, ...],
|
||||
**kwargs: t.Any,
|
||||
) -> t.Mapping[str, object]:
|
||||
# This method is needed, because the original __prepare__ method does not accept kwargs.
|
||||
return super().__prepare__(name, bases)
|
||||
return super().__prepare__(__name, __bases)
|
||||
|
||||
def __new__(
|
||||
metacls: t.Type[T], # type: ignore
|
||||
name: str,
|
||||
bases: t.Tuple[type, ...],
|
||||
namespace: t.Dict[str, t.Any],
|
||||
__name: str,
|
||||
__bases: t.Tuple[type, ...],
|
||||
__namespace: t.Dict[str, t.Any],
|
||||
**kwargs: t.Any,
|
||||
) -> T:
|
||||
# create new enum object
|
||||
cls = super().__new__(metacls, name, bases, namespace) # type: ignore
|
||||
cls: T = super().__new__(metacls, __name, __bases, __namespace) # type: ignore
|
||||
|
||||
# if the `TEnum` class is created, there are no parameters
|
||||
if len(kwargs) == 0:
|
||||
|
|
@ -44,9 +44,9 @@ class _EnumMeta(enum.EnumMeta):
|
|||
raise ValueError(f"unsupported parameter(s) detected: {unsupp_parm}")
|
||||
|
||||
# create construct format
|
||||
if TEnum in bases:
|
||||
if TEnum in __bases:
|
||||
enum_constr = TEnumConstruct(subcon, cls) # type: ignore
|
||||
elif TFlags in bases:
|
||||
elif TFlags in __bases:
|
||||
enum_constr = TFlagsConstruct(subcon, cls) # type: ignore
|
||||
else:
|
||||
enum_constr = None
|
||||
|
|
@ -74,7 +74,7 @@ class TEnum(enum.IntEnum, metaclass=_EnumMeta):
|
|||
def __init_subclass__(
|
||||
cls,
|
||||
subcon: "cs.Construct[t.Any, t.Any]",
|
||||
):
|
||||
) -> None:
|
||||
...
|
||||
|
||||
@classmethod
|
||||
|
|
@ -157,7 +157,7 @@ class TFlags(enum.IntFlag, metaclass=_EnumMeta):
|
|||
def __init_subclass__(
|
||||
cls,
|
||||
subcon: "cs.Construct[t.Any, t.Any]",
|
||||
):
|
||||
) -> None:
|
||||
...
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import construct_typed as cst
|
|||
Buffer = t.Union[bytes, memoryview, bytearray]
|
||||
ParsedType = t.TypeVar("ParsedType")
|
||||
BuildTypes = t.TypeVar("BuildTypes")
|
||||
ContainerType = t.TypeVar("ContainerType", bound=cst.TContainerMixin)
|
||||
ContainerType = t.TypeVar("ContainerType", bound=cst.DataclassStruct)
|
||||
T = t.TypeVar("T")
|
||||
|
||||
IdentType = t.TypeVar("IdentType")
|
||||
|
|
@ -20,7 +20,7 @@ def raises(
|
|||
) -> t.Union[t.Any, Exception]: ...
|
||||
@t.overload
|
||||
def common(
|
||||
format: cst.TStruct[ContainerType],
|
||||
format: ContainerType,
|
||||
datasample: Buffer,
|
||||
objsample: t.Union[ContainerType, t.Dict[str, t.Any]],
|
||||
sizesample: t.Union[int, t.Type[Exception]] = ...,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue