pyserde
pyserde copied to clipboard
Some NewTypes such as Dict do not work
from typing import NewType, Dict
from serde import deserialize, from_dict
@deserialize
class A:
a: Dict
assert from_dict(A, {'a': {'x': 'y'}}).a == {'x': 'y'}
MyType = NewType('MyType', Dict)
@deserialize
class B:
a: MyType
from_dict(B, {'a': {'x': 'y'}})
expected: B(a={'x': 'y'})
actual:
Traceback (most recent call last):
File "/usr/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 21, in <module>
File "/home/jenni/fnndsc/pman/venv/lib/python3.10/site-packages/serde/de.py", line 314, in from_dict
return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
File "/home/jenni/fnndsc/pman/venv/lib/python3.10/site-packages/serde/de.py", line 249, in from_obj
return serde_scope.funcs[FROM_DICT](o, reuse_instances=reuse_instances)
File "<string>", line 11, in from_dict
File "/home/jenni/fnndsc/pman/venv/lib/python3.10/site-packages/serde/core.py", line 155, in raise_unsupported_type
raise SerdeError(f"Unsupported type: {typename(type(obj))}")
serde.compat.SerdeError: Unsupported type: Dict
Info
$ python --version
Python 3.10.1
$ pip freeze | grep pyserde
pyserde==0.6.0
Hi @jennydaman
Thanks for the issue!
NewType
is currently supported in the case the underlying type is primitive.
I will look into supporting non primitives 👍
This would help me with:
- https://github.com/yukinarit/pyserde/discussions/323