madman-bob / python-dataclasses-serialization

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

default none values throws an exception #10

Closed DanielleFundbox closed 4 years ago

DanielleFundbox commented 4 years ago
class InfoWithNone:
    status: str
    product_types: List[str]
    id: int = None

info = InfoWithNone(status='active', product_types=['p1','p2'])
serialized_info = JSONSerializer.serialize(info)
deserialized_info = JSONSerializer.deserialize(InfoWithNone, serialized_info)

this throws
dataclasses_serialization.serializer_base.DeserializationError: Missing one or more required fields to deserialize
 {'status': 'active', 'product_types': ['p1', 'p2'], 'id': None} as <class 'tests.test_dataclasses_serialization.InfoWithNone'>
DanielleFundbox commented 4 years ago

original exception is dataclasses_serialization.serializer_base.DeserializationError: Cannot deserialize <class 'NoneType'> None to type <class 'int'>

madman-bob commented 4 years ago

Duplicate of https://github.com/madman-bob/python-dataclasses-serialization/issues/5.

None is not of type int, so it rightfully throws an error.

You want Optional[int].