On Fri, Aug 07, 2020 at 05:44:31PM -0400, Ricky Teachey wrote:
Would it make good semantic sense- and be useful- to specify valid numerical ranges using slices and type-hint syntax? My suggestion would be to, at minimum, provide this functionality for int and float.
We know that syntactically we can write an annotation like `int[1:10]`, even if it is a runtime TypeError. The question would be to ask mypy, and maybe some of the other type checkers, whether they are capable of and interested in doing static bounds checking.
Unless they are interested in the feature, having ints support it would be a waste of time.
For compatibility with slicing and range(), we would surely want int ranges to be half open:
int[1:100] # 1-99 inclusive, 100 exclused
but for floats, half-open intervals are a real pain, as you suggest. How do I specify an upper bound of exactly math.pi, say? A runtime check is easy:
x <= math.pi
but specifying it as an open interval requires me to know that 3.1415926535897936 is the next float greater than math.pi.