[Python-Dev] Updated version of PEP 526 (Syntax for Variable Annotations)

Guido van Rossum guido at python.org
Fri Sep 2 18:19:44 EDT 2016


We've prepared an updated version of PEP 526:
https://www.python.org/dev/peps/pep-0526/

This changes the title to "Syntax for Variable Annotations", now that
we've settled on global, class, instance, and local variables as the
things you might annotate.

There is one substantial change: where the previous version supported only

  NAME: TYPE
  TARGET: TYPE = VALUE

the new PEP removes the distinction and just allows

  TARGET: TYPE [= VALUE]

This simplifies the explanations a bit and enables type checkers to
support separating the annotation from the assignment for instance
variables in the __init__ method, e.g.

  def __init__(self):
      self.name: str
      if <some test>:
          self.name = <something>
      else:
          self.name = <something else>

The other changes are all minor editing nits, or clarifications about
the scope of the PEP. The scope clarification is important: while I
really want the new syntax settled in 3.6, I have no intention to pin
down the way type checkers use this syntax, apart from the observation
that

  TARGET: TYPE = VALUE

is just meant as a cleaner way to write what you'd currently write
using PEP 484 as

  TARGET = VALUE  # type: TYPE

The PEP does *not* claim that you have to use variable annotations --
in fact we'd prefer that they were unnecessary, but the prevalence of
type comments in code we've annotated so far makes it clear that there
are plenty of uses for them, and we'd rather have a clean syntax for
them that tools can see in the AST.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list