[Python-ideas] Optional static typing -- the crossroads

Łukasz Langa lukasz at langa.pl
Mon Aug 18 01:27:56 CEST 2014


On Aug 17, 2014, at 3:53 AM, Andrew Barnert <abarnert at yahoo.com.dmarc.invalid> wrote:

> The obvious way that already works (with MyPy, and also with ABCs for isinstance checking):
> 
>    class HashableIterable(Iterable, Hashable): pass
> 
>    def spam(a: HashableIterable):
>        pass

No. Specifying a subclass of Iterable and Hashable cannot possibly mean that *any* type that is both Iterable and Hashable is behaving like your subclass. 

>>> from collections import *
>>> class IterableHashable(Iterable, Hashable): pass
...
>>> isinstance("str", Iterable)
True
>>> isinstance("str", Hashable)
True
>>> isinstance("str", IterableHashable)
False

We would need a new construct like Union, maybe named AnySubclass, that checks True when all its base classes are in a given type's MRO. Some ABCs don't explicitly appear in a type's MRO but I fixed this problem with singledispatch.

-- 
Best regards,
Łukasz Langa

WWW: http://lukasz.langa.pl/
Twitter: @llanga
IRC: ambv on #python-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140817/721c777c/attachment.html>


More information about the Python-ideas mailing list