[Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Wed Aug 31 06:09:21 EDT 2016


On 31 August 2016 at 15:40, Guido van Rossum <guido at python.org> wrote:

On Tuesday, August 30, 2016, Nick Coghlan <ncoghlan at gmail.com> wrote:

What if we included local variable annotations in func.annotations as cells, like the entries in func.closure?

We could also use that as a micro-optimisation technique: once the type annotation cell is populated, CPython would just use it, rather than re-evaluating the local variable type annotation expression every time the function is called. But what runtime use have the annotations on locals? They are not part of any inspectable interface. I don't want to spend any effort on them at runtime. (Just the bit that they are treated as locals.)

I guess as long as they're included somewhere in the AST for the function body, I don't mind if the translation to bytecode throws them away - that's essentially saying that a function level type annotation is effectively interpreted as if it was:

if False:
    __annotations__[<varname>] = <annotation>

So the code generator will pick up syntax errors during normal execution, but not runtime errors (since the expression never actually gets evaluated).

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list