Skip to content

Commit 9e0b11e

Browse files
annotations: expand documentation on "simple" assignment targets (#120535)
This behavior is rather surprising and it was not clearly specified. Co-authored-by: Alex Waygood <[email protected]>
1 parent 6f63dff commit 9e0b11e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Doc/library/ast.rst

+7-3
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,13 @@ Statements
891891
An assignment with a type annotation. ``target`` is a single node and can
892892
be a :class:`Name`, a :class:`Attribute` or a :class:`Subscript`.
893893
``annotation`` is the annotation, such as a :class:`Constant` or :class:`Name`
894-
node. ``value`` is a single optional node. ``simple`` is a boolean integer
895-
set to True for a :class:`Name` node in ``target`` that do not appear in
896-
between parenthesis and are hence pure names and not expressions.
894+
node. ``value`` is a single optional node.
895+
896+
``simple`` is always either 0 (indicating a "complex" target) or 1
897+
(indicating a "simple" target). A "simple" target consists solely of a
898+
:class:`Name` node that does not appear between parentheses; all other
899+
targets are considered complex. Only simple targets appear in
900+
the :attr:`__annotations__` dictionary of modules and classes.
897901

898902
.. doctest::
899903

Doc/reference/simple_stmts.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,18 @@ statement, of a variable or attribute annotation and an optional assignment stat
333333

334334
The difference from normal :ref:`assignment` is that only a single target is allowed.
335335

336-
For simple names as assignment targets, if in class or module scope,
336+
The assignment target is considered "simple" if it consists of a single
337+
name that is not enclosed in parentheses.
338+
For simple assignment targets, if in class or module scope,
337339
the annotations are evaluated and stored in a special class or module
338340
attribute :attr:`__annotations__`
339341
that is a dictionary mapping from variable names (mangled if private) to
340342
evaluated annotations. This attribute is writable and is automatically
341343
created at the start of class or module body execution, if annotations
342344
are found statically.
343345

344-
For expressions as assignment targets, the annotations are evaluated if
346+
If the assignment target is not simple (an attribute, subscript node, or
347+
parenthesized name), the annotation is evaluated if
345348
in class or module scope, but not stored.
346349

347350
If a name is annotated in a function scope, then this name is local for

0 commit comments

Comments
 (0)