Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Literal #45

Open
mthuurne opened this issue Nov 21, 2023 · 0 comments
Open

Add support for Literal #45

mthuurne opened this issue Nov 21, 2023 · 0 comments

Comments

@mthuurne
Copy link
Member

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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant