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

Ryan Hiebert ryan at ryanhiebert.com
Wed Aug 20 16:27:47 CEST 2014


> 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. This feature would not available for applications that wish to support Python 2.

However, that stipulation isn’t unique to this feature. If you want be able to run on older versions of the language, you must forgo the features that are available in newer versions of the language.

You can’t use with_statement unless you’re willing to drop Python 2.4 support, you can’t use print_function unless you’re willing to drop 2.5 support, and you can’t use annotations unless you’re willing to drop all Python 2 support. This is the nature of language improvements.


More information about the Python-ideas mailing list