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

Fails when using Optional annotation for a #4

Closed
ivergara opened this issue May 8, 2019 · 1 comment
Closed

Fails when using Optional annotation for a #4

ivergara opened this issue May 8, 2019 · 1 comment

Comments

@ivergara
Copy link

ivergara commented May 8, 2019

Subject of the issue

If the dataclass with the TypedJsonMixin has a attribute with None as default value, and you want to properly annotate for mypy, the dataclass becomes unusable.

Steps to reproduce

Here is the code to reproduce the situation

from dataclasses import dataclass
from typing import Optional
from typed_json_dataclass import TypedJsonMixin

@dataclass
class Person(TypedJsonMixin):
    name: str
    age: Optional[int] = None

Person("John", 42)

Expected behaviour

Would have expected it to still be usable. Perhaps it is desired behavior due to how json is specified, and as such a non-issue. But maybe you're still interested in such a report.

Actual behaviour

Ends up raising

TypeError: typing.Union cannot be used with isinstance()

because under the hood Optional is a particular type Union[NonType, int].

Post Scriptum: Nice project and very clear coding! Good job.

@abatilo abatilo closed this as completed in f71809f Jun 3, 2019
@abatilo
Copy link
Owner

abatilo commented Jun 3, 2019

Hey @ivergara!

Thank you so much for taking the time to report this issue. I've addressed this in the following commit:
f71809f

And version 0.2.2 should be available on PyPI shortly.

Reminder that the to_dict and to_json methods have a keyword parameter for keep_none for handling whether or not you want to expose that some of these variables are in fact undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants