Question

Python explicit values for int variables

  • 13 April 2022
  • 1 reply
  • 42 views

Userlevel 3
Badge +1
  • Active Contributor
  • 5 replies

Is there a way to type a set of explicit values (that are of type int)? For example, I want to type that a variable can be from the set {0, 1, 2}?  (the numbers are actually mapped from constants)


1 reply

Userlevel 2

You can use literal types with mypy if you’re in Python 3.8+: https://mypy.readthedocs.io/en/latest/literal_types.html

Reply