konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

DefaultValueNotFoundError for Optional fields #241

Open eroller opened 1 year ago

eroller commented 1 year ago

Is your feature request related to a problem? Please describe. There is no way to require a field that is of type Optional, because dacite gives it a default value (i.e. None) automatically. See https://github.com/konradhalas/dacite#optional-fields

Describe the solution you'd like A way to configure dacite to require fields which have an Optional type.

Describe alternatives you've considered I cannot think of a workaround to get a DefaultValueNotFoundError when converting a dictionary that has missing fields of an Optional type.

Additional context It feels like dacite should provide a mechanism to distinguish between these two dataclasses. Currently they both behave the same way and this is inconsistent compared to dataclasses where the fields are not of Optional type.

@dataclass
class MyRequiredFieldsThatMayHaveValueNone:
   name: str | None
@dataclass
class MyOptionsFieldsThatByDefaultHaveValueNone:
   name: str | None = None