ProtixIT / dataclass-binder

Python library to bind TOML data to dataclasses in a type-safe way
MIT License
13 stars 2 forks source link

Handling of optional fields with non-`None` defaults #31

Closed mthuurne closed 1 year ago

mthuurne commented 1 year ago

In TOML, unlike JSON and YAML, there is no null value. This means that an optional field (T | None annotation) will only be able to have a value of None when None is the default value.

For example, for the field foo: str | None = "bar":

I think it's best to reject unsupported constructs as early as possible, so when the Binder is created.

The only doubt I have is that if we ever add JSON support, forcing a None default on optional fields would be an unnecessary limitation. On the other hand, maybe it's better to focus first on making the best possible TOML binder and not worry about features that don't exist yet.

mthuurne commented 1 year ago

Labeling this as "bug" because the current behavior of serializing a None value to TOML is to silently omit it from the output, when means that it reverts to the default value. Therefore, if the default is not None, this alters the data on a round trip.