Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import from json string when using Optional #170

Open
hector-sab opened this issue Jun 24, 2020 · 0 comments
Open

Can't import from json string when using Optional #170

hector-sab opened this issue Jun 24, 2020 · 0 comments
Labels
bug Something isn't working

Comments

@hector-sab
Copy link

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 hector-sab added the bug Something isn't working label Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant