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

Manuel Cerón ceronman at gmail.com
Thu Aug 14 22:29:31 CEST 2014


On Thu, Aug 14, 2014 at 8:55 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> On Thu, Aug 14, 2014 at 12:28:26PM +0200, Manuel Cerón wrote:
>
> > One interesting feature of TypeScript is that it allows you to annotate
> > existing code without modifying it, by using external definition files.
> In
> > the JavaScript world, many people have contributed TypeScript annotation
> > files for popular JS libraries (http://definitelytyped.org/).
> >
> > I think this is possible in Python as well doing something like this:
> >
> > @annotate('math.ciel')
> > def ciel(x: float) -> int:
> >     pass
>
> I'm afraid I don't understand what the annotate decorator is doing here.
> Can you explain please?
>

The idea is to add type annotations to modules without modifying them. For
example, in this case, the stdlib math module is defined and implemented in
C, but you still want to have annotations for it so that if you write
math.ciel('foo'), the static type analyzer gives you a error or warning. By
defining a new module, for example math_annotations.py with empty functions
with annotated signatures, you can let the static analyzer know what are
the annotations for another module. In this example, the annotate decorator
is just a way of telling the static analyzer that these annotations apply
to the math.ceil function, not math_annotations.ceil.

This is what TypeScript does to annotate popular libraries written in plain
JavaScript with zero type information.

Manuel.



>
>
>
>
> --
> Steven
> _______________________________________________
> 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/20140814/5a07ff32/attachment.html>


More information about the Python-ideas mailing list