From 11a2e02210458aa0d9820623397f2a6cbe506331 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Fri, 1 Jan 2021 15:40:46 +0100 Subject: [PATCH] enforce correct types --- construct_typed/tstruct.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/construct_typed/tstruct.py b/construct_typed/tstruct.py index a416721..e6cb8f5 100644 --- a/construct_typed/tstruct.py +++ b/construct_typed/tstruct.py @@ -50,9 +50,7 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]): ) -> None: if not dataclasses.is_dataclass(dataclass_type): raise TypeError( - 'The class "{}" is not a "dataclasses.dataclass"'.format( - dataclass_type.__name__ - ) + "'{}' has to be a 'dataclasses.dataclass'".format(dataclass_type) ) self.dataclass_type = dataclass_type self.swapped = swapped @@ -68,7 +66,7 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]): subcon_fields[field.name] = field.metadata["subcon"] # init adatper - super(_TStruct, self).__init__(self._create_subcon(subcon_fields)) # type: ignore + super(_TStruct, self).__init__(self._create_subcon(subcon_fields)) def _create_subcon( self, subcon_fields: t.Dict[str, t.Any] @@ -100,18 +98,20 @@ class _TStruct(Adapter[t.Any, t.Any, ParsedType, BuildTypes]): return dc def _encode( - self, obj: ParsedType, context: "cs.Context", path: "cs.PathType" + self, obj: BuildTypes, context: "cs.Context", path: "cs.PathType" ) -> t.Dict[str, t.Any]: - # get all fields from the dataclass - fields = dataclasses.fields(self.dataclass_type) + if isinstance(obj, self.dataclass_type): + # get all fields from the dataclass + fields = dataclasses.fields(self.dataclass_type) - # extract all fields from the container, that are used for create the dataclass object - ret_dict = {} - for field in fields: - value = getattr(obj, field.name) - ret_dict[field.name] = value + # extract all fields from the container, that are used for create the dataclass object + ret_dict = {} + for field in fields: + value = getattr(obj, field.name) + ret_dict[field.name] = value - return ret_dict + return ret_dict + raise TypeError("'{}' has to be of type {}".format(obj, self.dataclass_type)) class TStruct(_TStruct[ParsedType, ParsedType]):