Skip to content

Add support for Literal #45

@mthuurne

Description

@mthuurne

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions