[Python-ideas] Proposal: Use mypy syntax for function annotations

Georg Brandl g.brandl at gmx.net
Sun Aug 24 10:00:32 CEST 2014


On 08/24/2014 07:54 AM, Greg Ewing wrote:
> Steven D'Aprano wrote:
>> I don't really understand what you're trying to say here, so I may be 
>> misinterpreting you. I *think* that you're trying to say that for every 
>> type in the standard library, and every class created by third parties 
>> (including subclasses), the author will have to "declare" (in some 
>> unknown sense) that it can be used for type annotations like MyList[T], 
>> for some type T.
> 
> I suppose the run-time incarnations of the type descriptions
> could be looser, but if you want to use them for static checking,
> the static checker is going to have to know what MyList[T] means
> in some detail (what effect the parameter has on the method
> types, etc.) The programmer will have to specify all that
> somehow.
> 
> The way this is done in other languages with static type
> checking is to give the class declaration a parameter list.
> I was envisaging that the mypy type description syntax would have
> something equivalent. Not sure what form it would take, though.

Exactly.  Does mypy handle that?  For example, for a custom mapping
type

class Mapping(object):
    def setitem(self, key, value):
        ...

how would one specify
a) that you can use Mapping[T1, T2] as a type annotation and
b) the type annotations for the "key" and "value" arguments?

Georg



More information about the Python-ideas mailing list