On Fri, Aug 7, 2020 at 6:01 PM Paul Moore <p.f.mo...@gmail.com> wrote:
> On Fri, 7 Aug 2020 at 22:46, Ricky Teachey <ri...@teachey.org> wrote: > > > > This was inspired by a tweet today from Brandon Rhodes. I looked for > something like it on the mypy issues page and didn't find anything. > > > > 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. > > > > Consider that currently we have: > > > > x: int # this means "all ints", today > > x: float # this means "all floating point numbers", today > > > > Idea in a nutshell would be for the following type declarations to mean: > > > > x: int[0:] # any ints greater than or equal to zero would match, others > would fail > > x: int[:101] # any ints less than 101 match > > x: int[0:101:2] # even less than 101 > > I suspect the biggest issue with this is that it's likely to be > extremely hard (given the dynamic nature of Python) to check such type > assertions statically. Even in statically typed languages, you don't > often see range-based types like this. And type assertions don't do > runtime checks, so if they can't be usefully checked statically, they > probably aren't going to be of much benefit (documentation is > basically all). > > Paul You could be right and I don't know much about this subject. However the question that comes up for me, though, is: how does TypedDict perform its static checks? It seems like this: class D(typing.TypedDict): a: int d: D = dict(b=2) # error Shouldn't be any harder to type check than this: x: int[0:] = -1 # error No? --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/JRAGJ5RUJI2IWKKKT6HQHDDLKOPGGPP5/ Code of Conduct: http://python.org/psf/codeofconduct/