You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
define a __post_init__() method that raises ValueError for 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.
The text was updated successfully, but these errors were encountered:
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:
typing.Literal
__post_init__()
method that raisesValueError
for values that are not allowedThe
__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.The text was updated successfully, but these errors were encountered: