ProtixIT / dataclass-binder

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

Add support for `Literal` #45

Open mthuurne opened 9 months ago

mthuurne commented 9 months ago

It might be useful to limit the allowed values, for example allow only 0-5 instead of arbitrary integers.

There are multiple ways to do this:

The __post_init__() approach is the most flexible, but it requires more code and it only adds a runtime check, not a static check. Also restrictions added in this way cannot automatically be included in generated documentation comments. So having an alternative mechanism would be useful.

An advantage of Literal over enumerations could be that it is more compact: it can be defined inline and does not require coming up with names for the type and all its values.