typed-json-dataclass icon indicating copy to clipboard operation
typed-json-dataclass copied to clipboard

Can't import from json string when using Optional

Open hector-sab opened this issue 4 years ago • 0 comments

Subject of the issue

When using the Optional type hint, I can no longer convert from a json string into the data class.

Steps to reproduce

The following code will trow

TypeError: A.c was defined to be any of: (<class '__main__.B'>, <class 'NoneType'>) but was found to be <class 'dict'> instead
from typing import List, Optional
from dataclasses import dataclass
from typed_json_dataclass import TypedJsonMixin

@dataclass
class B(TypedJsonMixin):
    a: float = 0.0
    b: float = 0.0
    c: float = 0.0

@dataclass
class A(TypedJsonMixin):
    a: Optional[int] = None
    b: Optional[str] = None
    c: Optional[B] = None

b = B(1.0, 1.0, 1.0)
a = A(1, "a", b)
c = A.from_json(a.to_json())

Expected behaviour

Create a data class of type A

Actual behaviour

Does not create the dataclass of type A from the json and trows an error.

hector-sab avatar Jun 24 '20 14:06 hector-sab