konradhalas / dacite

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

class with field(init=False) raises error #13

Closed bpeake-illuscio closed 5 years ago

bpeake-illuscio commented 5 years ago

Currently, using a dataclasses which include non-init fields throws an error.

Example (python 3.7):

>>> import dacite
>>> from dataclasses import dataclass, field
>>> 
>>> @dataclass
>>> class A:
...     number: int
...     text: str
... 
...     post: str = field(init=False)
... 
>>>
>>> data = {
...     "_data_type": "A",
...     "number": 1,
...     "text": "hello",
...     "post": "gotcha!"
... }
>>> dacite.from_dict(A, data)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/williampeake/venvs/isle_collections-py-37/lib/python3.7/site-packages/dacite.py", line 96, in from_dict
    return data_class(**values)
TypeError: __init__() got an unexpected keyword argument 'post'
bpeake-illuscio commented 5 years ago

Made a PR that fixes this here: https://github.com/konradhalas/dacite/pull/14

konradhalas commented 5 years ago

Fixed by https://github.com/konradhalas/dacite/commit/686570b899c1fd36062dda0e886af282d93911e2