[Python-ideas] Optional static typing -- the crossroads
Greg Ewing
greg.ewing at canterbury.ac.nz
Mon Aug 18 02:04:20 CEST 2014
Andrew Barnert 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
That way might be obvious, but it's wrong. It says that spam()
only takes an instance of the specific class HashableIterable
or a subclass thereof. It won't accept anything else, even if
it implements Iterable and Hashable perfectly well.
>
> 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, {})
Automating it won't make it any more right. There needs to
be a distinct concept such as Intersection() as a counterpart
to Union().
--
Greg
More information about the Python-ideas
mailing list