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

Gregory Salvan apieum at gmail.com
Wed Aug 20 16:34:06 CEST 2014


In mypy FAQ: http://www.mypy-lang.org/faq.html

"All of my code is still in Python 2. What are my options?
 Mypy currently supports Python 3 syntax. Python 2 support is still in
early stages of development. However, Python 2 support will be improving."

I don't know how.
Does someone know if a decorator that add "__annotations__" attribute to
the function can do the job ?

with something like that:


  @mypy(int, int, returns=int)
  def gcd(a, b):
     <tralala>



2014-08-20 16:11 GMT+02:00 Brett Cannon <brett at python.org>:

>
>
> On Wed Aug 20 2014 at 9:29:34 AM Paul Moore <p.f.moore at gmail.com> wrote:
>
>> On 14 August 2014 00:30, Guido van Rossum <guido at python.org> wrote:
>> > We certainly *could* do that. However, I haven't seen sufficient other
>> uses
>> > of annotations. If there is only one use for annotations (going
>> forward),
>> > annotations would be unambiguous. If we allow different types of
>> > annotations, there would have to be a way to tell whether a particular
>> > annotation is intended as a type annotation or not. Currently mypy
>> ignores
>> > all modules that don't import typing.py (using any form of import
>> > statement), and we could continue this convention. But it would mean
>> that
>> > something like this would still require the typing import in order to be
>> > checked by mypy:
>> >
>> > import typing
>> >
>> > def gcd(int a, int b) -> int:
>> >     <tralala>
>>
>> Sorry, I'm slowly going through this thread, so my apologies if this
>> has been covered later, but it seems to me that projects (and in
>> particular libraries) that want to target 2.7 as well as 3.x will be
>> forced to avoid this feature. And avoid any dependencies that use it.
>>
>> 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).
>>
>
> I suspect the answer is "you don't". Just like everything else that is
> syntactically exclusive to Python 3, it's a perk you get with Python 3-only
> code that you simply can't get if you want to maintain
> backwards-compatibility.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140820/182a9788/attachment.html>


More information about the Python-ideas mailing list