[Python-ideas] Proposal: Use mypy syntax for function annotations
Bob Ippolito
bob at redivi.com
Thu Aug 14 22:58:45 CEST 2014
On Thu, Aug 14, 2014 at 1:44 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 08/14/2014 01:29 PM, Manuel Cerón wrote:
>
>> On Thu, Aug 14, 2014 at 8:55 PM, Steven D'Aprano wrote:
>>
>>> On Thu, Aug 14, 2014 at 12:28:26PM +0200, Manuel Cerón wrote:
>>>
>>>>
>>>>
>>>> @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.
>>
>
> To make sure I understand: The above snippet is located in a file named
> 'math_annotations.py', and the annotate decorator says "the following
> function annotation should be stored against the 'ceil' function in the
> 'math' module, not the 'ceil' function in this current module" ?
>
mypy has existing infrastructure in the form of stub modules to support
this use case:
https://github.com/JukkaL/mypy/blob/master/stubs/3.2/math.py
I'm not sure if stubs can easily be pulled in from outside of mypy, but
that would be straightforward to support if it doesn't work already.
-bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140814/b1a13d2a/attachment.html>
More information about the Python-ideas
mailing list