madman-bob / python-dataclasses-serialization

Serialize/deserialize Python dataclasses to various other data formats
MIT License
25 stars 11 forks source link

Cannot deserialize list #14

Open remdragon opened 3 years ago

remdragon commented 3 years ago

dataclasses_serialization.version== '1.3.1' Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:30:23) [MSC v.1928 32 bit (Intel)] on win32 Windows 10 Pro Version 2004, Build 19041.867

Here's what I've narrowed it down to:

from dataclasses import dataclass, field
from dataclasses_serialization.json import JSONSerializer # type: ignore # pip install dataclasses-serialization
from typing import List

@dataclass
class LogModule:
    name: str
    level: str

@dataclass
class Logging:
    modules: list[LogModule] = field ( default_factory = list )

@dataclass
class MyConfig:
    logging: Logging = Logging (
        modules = [
            LogModule ( '__main__', 'DEBUG' ),
        ],
    )

myconfig = MyConfig()
x = JSONSerializer.serialize ( myconfig )
dup = JSONSerializer.deserialize ( MyConfig, x )
print ( repr ( dup ) )
assert dup == myconfig, f'{dup=} != {myconfig=}'

produces the following output:

Traceback (most recent call last):
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\serializer.py", line 62, in deserialize
    deserialization_func = self.deserialization_functions[cls]
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\refinement_dict.py", line 63, in __getitem__
    return self.fallback[key]
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\refinement_dict.py", line 65, in __getitem__
    raise KeyError(f"{key!r}")
KeyError: 'list[__main__.LogModule]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\dataclasses.py", line 29, in dict_to_dataclass
    **{
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\dataclasses.py", line 30, in <dictcomp>
    fld.name: deserialization_func(fld_type, dct[fld.name])
  File "C:\Python39-32\lib\site-packages\toolz\functoolz.py", line 303, in __call__
    return self._partial(*args, **kwargs)
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\serializer.py", line 64, in deserialize
    raise DeserializationError("Cannot deserialize type {}".format(cls))
dataclasses_serialization.serializer_base.errors.DeserializationError: Cannot deserialize type list[__main__.LogModule]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\dataclasses.py", line 29, in dict_to_dataclass
    **{
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\dataclasses.py", line 30, in <dictcomp>
    fld.name: deserialization_func(fld_type, dct[fld.name])
  File "C:\Python39-32\lib\site-packages\toolz\functoolz.py", line 303, in __call__
    return self._partial(*args, **kwargs)
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\serializer.py", line 66, in deserialize
    return deserialization_func(cls, serialized_obj)
  File "C:\Python39-32\lib\site-packages\toolz\functoolz.py", line 303, in __call__
    return self._partial(*args, **kwargs)
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\dataclasses.py", line 36, in dict_to_dataclass
    raise DeserializationError(
dataclasses_serialization.serializer_base.errors.DeserializationError: Missing one or more required fields to deserialize {'modules': [{'name': '__main__', 'level': 'DEBUG'}]} as <class '__main__.Logging'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\cvs\itas\test_dcs.py", line 24, in <module>
    dup = JSONSerializer.deserialize ( MyConfig, x )
  File "C:\Python39-32\lib\site-packages\toolz\functoolz.py", line 303, in __call__
    return self._partial(*args, **kwargs)
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\serializer.py", line 66, in deserialize
    return deserialization_func(cls, serialized_obj)
  File "C:\Python39-32\lib\site-packages\toolz\functoolz.py", line 303, in __call__
    return self._partial(*args, **kwargs)
  File "C:\Python39-32\lib\site-packages\dataclasses_serialization\serializer_base\dataclasses.py", line 36, in dict_to_dataclass
    raise DeserializationError(
dataclasses_serialization.serializer_base.errors.DeserializationError: Missing one or more required fields to deserialize {'logging': {'modules': [{'name': '__main__', 'level': 'DEBUG'}]}} as <class '__main__.MyConfig'>

If I change the definition of Logging.modules from list[LogModule] to List[LogModule] then it works correctly.