pyserde icon indicating copy to clipboard operation
pyserde copied to clipboard

Some NewTypes such as Dict do not work

Open jennydaman opened this issue 2 years ago • 2 comments

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

jennydaman avatar Jan 23 '22 20:01 jennydaman

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 👍

yukinarit avatar Jan 24 '22 12:01 yukinarit

This would help me with:

  • https://github.com/yukinarit/pyserde/discussions/323

davetapley avatar Mar 02 '23 17:03 davetapley