konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 107 forks source link

WrongTypeError: should be "typing.Union[float, NoneType]" instead of "int" #77

Closed Garrett-R closed 4 years ago

Garrett-R commented 4 years ago

This is quite similar to Issue #62, only different being that I've now added an extra Optional:

import dataclasses
from typing import Optional

import dacite

@dataclasses.dataclass
class Person:
    height: Optional[float] = 160

person = Person()
person_dict = dataclasses.asdict(person)

new_person_1 = dacite.from_dict(data_class=Person, data=person_dict)

On the latest version of Dacite (v1.2.0 ‒ 9c311b1), executing this yields:

WrongTypeError: wrong type for field "height" - should be "typing.Union[float, NoneType]" instead of "int"

Changing Optional[float] to be just float will make it work again. This seems to be a bug since Optional[float] should also work.

konradhalas commented 4 years ago

@Garrett-R thank you for reporting this issue :)

You have right - it's a bug. Fixed and deployed to PyPI. Please check the newest version (1.2.1).

Garrett-R commented 4 years ago

Woohoo! Thanks!!