[Python-ideas] PEP 484 (Type Hints) -- first draft round

Stefan Behnel stefan_ml at behnel.de
Wed Jan 21 08:18:29 CET 2015


Greg Ewing schrieb am 19.01.2015 um 21:28:
>> On 1/18/2015 10:40 PM, Guido van Rossum wrote:
>>
>>  > Very few people subclass builtins.list ...
>>  > So perhaps Cython could just assume that typing.List means
>>  > builtins.list and (dynamically) reject calls that pass a subclass
>>  > of builtins.list?
> 
> That's actually a reasonable idea. Anything that accepts
> something as weird as a trianglular matrix implemented as
> a list subclass will probably accept any kind of sequence,
> so you might as well type it as Sequence (or whatever the
> abstract sequence interface is to be called).

Well, that's what Cython currently does, based on its own type declaration
semantics for builtin types. I agree that subtyping is rare for lists, but
it's quite common for dicts, including defaultdict, OrderedDict, or even
YourOwnMissingKeyDictImpl. And there's set and frozenset, so if you type
something as "set" (which no-one does, really), it would have to allow
frozenset as well in most (read-only) cases (or maybe the other way round).
But set and frozenset do not inherit from each other, so instead of typing
something as "set", it would have to be the "Set" or "MutableSet" ABC type,
which then is uninteresting again for any C level optimisation, as it's way
too broad and there's no C-API for it.

There's really a visible gap between what's helpful at a Python type
checking level and what's a helpful compiler type hint for Cython and CPython.

Stefan




More information about the Python-ideas mailing list