konradhalas / dacite

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

Support for NewType #33

Closed jurajpelikan closed 5 years ago

jurajpelikan commented 5 years ago

Example:

from dataclasses import dataclass
from typing import NewType

import dacite

MyStr = NewType("MyStr", str)

@dataclass
class Data:
    my_str: MyStr

dacite.from_dict(Data, {"my_str": "foo-bar"})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    dacite.from_dict(Data, {"my_str": "foo-bar"})
  File "dacite.py", line 103, in from_dict
    if not _is_instance(field.type, value):
  File "dacite.py", line 337, in _is_instance
    return isinstance(value, t)
TypeError: isinstance() arg 2 must be a type or tuple of types

Dacite currently does not support the "NewType" fields. The problem is in the _is_instance function. Please see the related pull request.

konradhalas commented 5 years ago

@jurajpelikan thank you very much for reporting this issue. Due to my latest "big" refactor your PR has conflicts, I had to resolve them manually.

Fixed in 9a6b27171b7d3df32c609f763c3ba3b141cb377a