-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
enhancementNew feature or requestNew feature or request
Description
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:
- annotate the field with an enumeration type (see Support enumerations #29)
- annotate the field using
typing.Literal - define a
__post_init__()method that raisesValueErrorfor values that are not allowed
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.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request