diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-06-15 15:35:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-15 15:35:57 (GMT) |
commit | 29bbd5f8e41217d53ac5eb5cd05f58477b5baa59 (patch) | |
tree | a9a79cb14739ea1551a916f6317e21d7ece23de7 | |
parent | cbcb5265bfaf80af386faa8858359bb6f15cb77d (diff) | |
download | cpython-29bbd5f8e41217d53ac5eb5cd05f58477b5baa59.zip cpython-29bbd5f8e41217d53ac5eb5cd05f58477b5baa59.tar.gz cpython-29bbd5f8e41217d53ac5eb5cd05f58477b5baa59.tar.bz2 |
[3.13] annotations: expand documentation on "simple" assignment targets (GH-120535) (#120555)
This behavior is rather surprising and it was not clearly specified.
(cherry picked from commit 9e0b11eb21930b7b8e4a396200a921e9985cfca4)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r-- | Doc/library/ast.rst | 10 | ||||
-rw-r--r-- | Doc/reference/simple_stmts.rst | 7 |
2 files changed, 12 insertions, 5 deletions
diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index d4ccf28..c5daa6e 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -891,9 +891,13 @@ Statements An assignment with a type annotation. ``target`` is a single node and can be a :class:`Name`, a :class:`Attribute` or a :class:`Subscript`. ``annotation`` is the annotation, such as a :class:`Constant` or :class:`Name` - node. ``value`` is a single optional node. ``simple`` is a boolean integer - set to True for a :class:`Name` node in ``target`` that do not appear in - between parenthesis and are hence pure names and not expressions. + node. ``value`` is a single optional node. + + ``simple`` is always either 0 (indicating a "complex" target) or 1 + (indicating a "simple" target). A "simple" target consists solely of a + :class:`Name` node that does not appear between parentheses; all other + targets are considered complex. Only simple targets appear in + the :attr:`__annotations__` dictionary of modules and classes. .. doctest:: diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index a253482..4f6c0c6 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -333,7 +333,9 @@ statement, of a variable or attribute annotation and an optional assignment stat The difference from normal :ref:`assignment` is that only a single target is allowed. -For simple names as assignment targets, if in class or module scope, +The assignment target is considered "simple" if it consists of a single +name that is not enclosed in parentheses. +For simple assignment targets, if in class or module scope, the annotations are evaluated and stored in a special class or module attribute :attr:`__annotations__` that is a dictionary mapping from variable names (mangled if private) to @@ -341,7 +343,8 @@ evaluated annotations. This attribute is writable and is automatically created at the start of class or module body execution, if annotations are found statically. -For expressions as assignment targets, the annotations are evaluated if +If the assignment target is not simple (an attribute, subscript node, or +parenthesized name), the annotation is evaluated if in class or module scope, but not stored. If a name is annotated in a function scope, then this name is local for |