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

Terry Reedy tjreedy at udel.edu
Wed Aug 20 21:40:29 CEST 2014


On 8/20/2014 10:27 AM, Ryan Hiebert wrote:
>
>> On Aug 20, 2014, at 8:28 AM, Paul Moore <p.f.moore at gmail.com>
>> wrote:
>>
>> Specifically, the annotation syntax is purely Python 3.x, so
>> without some form of translation or import hook, 2.7 won't accept
>> it. And adding a dependency (even if it's only a very lightweight
>> typing.py plus a hook installed somewhere/somehow) for something
>> that only has value as part of a developer-level type check doesn't
>> seem like a good solution.
>>
>> So, I'd like someone to explain (maybe by pointing me to relevant
>> mypy docs, I haven't investigated them yet) how I should modify my
>> existing code that supports 2.7 and 3.x so that it uses the new
>> functionality *without* extra runtime dependencies that only
>> deliver build/test-time benefits (that's the problem I always had
>> with setuptools/pkg_resources, and I'd not like to see it repeated
>> here).
>
> As you’ve suggested, using annotations won’t work on Python 2 where
> there are no annotations.

Correct.

> This feature would not available for applications that wish to support Python 2.

Not exactly true, which is to say, true for run-time use, not true for 
pre-compile call checking.  Guido mostly wants type hints for the 
latter. Call checking mostly just needs to be done once (on 3.5+).

To expand on what I said elsewhere: suppose a 2&3 app A uses 2&3 library 
L, which has a separate annotation file.  You run the call checks on 
3.5.  Assuming that the 2&3 code of each use bytes and unicode and not 
str, calls should be good on all versions.  (They will be unless the 
same code produces objects of different types on different versions.)

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list