[Python-ideas] Optional static typing -- the crossroads
Andrew Barnert
abarnert at yahoo.com
Sun Aug 17 12:53:47 CEST 2014
On Aug 17, 2014, at 2:33, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Stefan Behnel wrote:
>> However, it does not support protocols, so it still needs
>> something that allows us to say Iterable(int) in some way.
>
> Just had a thought -- does mypy provide a way to express
> a type that supports more than one protocol? E.g. can you
> say that something must be both Iterable and Hashable?
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
But, there's no reason typing.py couldn't add a wrapper that does this automatically:
class Multiple:
@staticmethod
def __getitem__(*types);
return type(types[0])(
'_'.join(t.__name__ for t in types), types, {})
def spam(a: Multiple[Iterable, Hashable]):
pass
And, on analogy with the proposal for | as a shortcut for Union, the base class could add:
def __and__(self, other):
return Multiple[self, other]
def spam(a: Iterable & Hashable):
pass
More information about the Python-ideas
mailing list