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

Ethan Furman ethan at stoneleaf.us
Thu Aug 14 22:44:56 CEST 2014


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" ?

--
~Ethan~


More information about the Python-ideas mailing list