Skip to content

Commit

Permalink
Handle init=False fields with a missing value. Fixes konradhalas#44.
Browse files Browse the repository at this point in the history
  • Loading branch information
konradhalas committed Apr 13, 2019
1 parent 99c3723 commit fed8293
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dacite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None)
try:
value = get_default_value_for_field(field)
except DefaultValueNotFoundError:
if not field.init:
continue
raise MissingValueError(field.name)
if field.init:
init_values[field.name] = value
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ class X:
assert result == x


def test_from_dict_with_post_init_missing_value():
@dataclass
class X:
s: str = field(init=False)

result = from_dict(X, {})

assert not hasattr(result, "s")


def test_from_dict_with_new_type():
MyStr = NewType("MyStr", str)

Expand Down

0 comments on commit fed8293

Please sign in to comment.