Closed thincal closed 1 year ago
@thincal thank you for reporting this issue, but TBH I don't like this approach. If b
can be None
you should reflect this in your types.
Working example:
import dataclasses
from typing import Optional
import dacite
@dataclasses.dataclass
class Example:
a: int
b: Optional[str]
assert Example(a=1, b=None) == dacite.from_dict(Example, {"a": 1})
If incoming dict misses some fields, the
dacite.from_dict
would raise theMissingValueError
exception.To let it work, either the dict must be carrying with complete fields or the dataclass be declared with default value, either way requires a bit of work.
For example:
Proposal solution:
adding a
allow_missing_fields_as_none
option into Config, such as: