I use type hints with pydantic to set return schema for my Python API.
I would like to write a literal type that allows the numbers 0 to 100. This is easy typing it out manually:
from typing import Literal
MyType = Literal[0, 1, 2, ... , 99, 100]
This is not particularly pythonic and I'm looking for a shorthand, essentially:
Literal[range(101)]
Unfortunately the above expects the literal value that is range(101)
. I have also tried:
Literal[list(range(101))]
Literal[0:101]
However these fail as list
and slice
are unhashable types.
How do I do this without typing out the numbers 0 through 100?