def decode(self, data: dict, field_type: typing.Type) -> typing.Any: origin_args = typing.get_args(field_type) if data is None: return None return self.transcoder.decode(data, origin_args[0])
def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any: origin_args = typing.get_args(field_type) if obj is None: return None return self.transcoder.encode(obj, origin_args[0])
def decode(self, data: dict, field_type: typing.Type) -> typing.Any: origin_args = typing.get_args(field_type) return { self.trancoder.decode(k, origin_args[0]): self.trancoder.decode(v, origin_args[1]) for k, v in data.items() }
def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any: origin_args = typing.get_args(field_type) return { self.trancoder.encode(k, origin_args[0]): self.trancoder.encode(v, origin_args[1]) for k, v in obj.items() }
def can_handle(self, field_type: typing.Type) -> bool: origin = typing.get_origin(field_type) origin_args = typing.get_args(field_type) return ( # typing.Optional[some_type] origin is typing.Union and len(origin_args) == 2 and origin_args[1] is type(None) # noqa: E721 )
def decode(self, data: dict, field_type: typing.Type) -> typing.Any: origin = typing.get_origin(field_type) or field_type origin_args = typing.get_args(field_type) return origin(self.trancoder.decode(o, origin_args[0]) for o in data)
def encode(self, obj: typing.Any, field_type: typing.Type) -> typing.Any: origin_args = typing.get_args(field_type) return [self.trancoder.encode(o, origin_args[0]) for o in obj]
def can_handle(self, field_type: typing.Type) -> bool: origin = typing.get_origin(field_type) or field_type origin_args = typing.get_args(field_type) return (origin in (list, tuple) and len(origin_args) == 2 and origin_args[1] == Ellipsis)