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

Ethan Furman ethan at stoneleaf.us
Mon Aug 25 01:59:02 CEST 2014


On 08/23/2014 04:00 PM, Antoine Pitrou wrote:
>
> Le 23/08/2014 13:25, Steven D'Aprano a écrit :
>>>
>>> It's not a new use. A type class is a class, and calling it is just
>>> instantiating that class. There's nothing new here. If you think that's
>>> a bit "meta", it's no different than e.g. higher-order functions.
>>
>> There's no instantiation during *static* analysis, because the code
>> hasn't run yet.
>
> In your idea of "static analysis", it hasn't. Because you think it should involve some kind of separate syntax analysis
> tool that has nothing to do with regular Python. But Python is powerful enough to let you do that using normal
> introspection of modules.
>
> And it's *exactly* how we are exposing function annotations (and also docstrings, etc.): using runtime-accessible
> introspection information which is gathered by importing modules and therefore actually *executing* toplevel module
> code. Not merely compiling it.

Is this workable?  If I have a nested class in a function:

   def spam():
      class fribble:
         def __init__(self, x:int):
            blahblah

I would expect MyPy to be able to retrieve and use the annotations from fribble without actually calling spam.  If 
static checking is only available at the top level I think a lot of power/usefulness is gone.

--
~Ethan~


More information about the Python-ideas mailing list