classification
Title: Doc: Assignment statement to tuple or list: case missing.
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, emilyemorehouse, gvanrossum, martin.panter, mdk, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-06-16 13:01 by mdk, last changed 2018-11-12 00:37 by mdk. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7762 merged mdk, 2018-06-17 10:47
PR 10444 closed pablogsal, 2018-11-10 04:15
PR 10472 merged miss-islington, 2018-11-11 23:59
PR 10473 merged miss-islington, 2018-11-11 23:59
Messages (9)
msg319735 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2018-06-16 13:01
In [1] I read:

> If the target list is a comma-separated list of targets, or a single target in square brackets

This come from https://bugs.python.org/issue23275 (patch is [2]).

I suspect there's a missing case "list of targets in square brackets" (and to be pedantic "list of targets in parenthesis").

The specialized documentation about single-valued targets in sequence-like construction come from this difference:

    >>> (a) = 42
    >>> a
    42

    >>> [a] = [42]
    >>> a
    42


which is correctly described the line before:

> If the target list is a single target in parentheses: The object is assigned to that target.

So the correct way to state it may be:

> Else the object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

The `Else`, coupled with the existing "Assignment of an object to a target list, optionally enclosed in parentheses or square brackets" covers properly the cases:

- Multiple target separated by comas (already covered)
- Multiple target enclosed by parenthesis and brackets (not covered)
- Single target enclosed by angle brackets (already covered)


[1]: https://docs.python.org/3.7/reference/simple_stmts.html#assignment-statements
[2]: https://bugs.python.org/file42878/issue23275_v4.diff
msg319737 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-06-16 13:32
I think I intended the third option to include all comma-separated lists, including:

a, b, c = x  # No brackets
(a, b, c) = x  # Round brackets
[a, b, c] = x  # Square brackets
a, = x  # Single target with comma

Perhaps something like this would be clearer:

* If the target list is
    * a comma-separated list of targets, with or without brackets, or
    * a single target with a trailing comma, with or without brackets, or
    * a single target in square brackets,
  the object must be . . .

The empty target list case could also be merged. Then the only differentiation is between a single target (no comma, optional round brackets) and iterable unpacking.
msg319743 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2018-06-16 14:52
Agree, the empty list could be merged, the only special-case is the "single item in parenthesis", as always.

I'm still exploring the path to simplicity over explicitly listing everything (but in the reference, explicitly listing could be what we need):

    Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows.

        If the target list is a single target in parentheses: The object is assigned to that target.
        Else the object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

What do you think? Should we go full-explicit, with trailing comas and everything?
msg319797 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-06-16 23:52
I think it is okay to leave out the options for the unpacking case. But I think it is worth clarifying that the single-target case also applies without parentheses, but that it doesn’t apply if there is a trailing comma. So:

‘‘‘
If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target.

Else the object must be an iterable . . .
’’’
msg329511 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-09 12:27
I think that the fact that `(a) = 42` is accepted is rather an implementation detail, and the consequence of limitations of the grammar parser. It accepts arbitrary expression at the left hand side of assignment. After transforming CST to AST unsuitable targets are rejected, but information about grouping parenthesis is lost at this stage.

This can be fixed if check the left hand side node before converting to AST.
msg329554 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-11-09 20:13
I would like to see this fixed.

On Fri, Nov 9, 2018 at 4:27 AM Serhiy Storchaka <report@bugs.python.org>
wrote:

>
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
>
> I think that the fact that `(a) = 42` is accepted is rather an
> implementation detail, and the consequence of limitations of the grammar
> parser. It accepts arbitrary expression at the left hand side of
> assignment. After transforming CST to AST unsuitable targets are rejected,
> but information about grouping parenthesis is lost at this stage.
>
> This can be fixed if check the left hand side node before converting to
> AST.
>
> ----------
> nosy: +gvanrossum, serhiy.storchaka
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue33878>
> _______________________________________
>
-- 
--Guido (mobile)
msg329697 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2018-11-11 23:59
New changeset 082875dcd6d482558e5f1da97a1c801d60b3ed5b by Julien Palard in branch 'master':
bpo-33878: Doc: Fix missing case by simplifying. (GH-7762)
https://github.com/python/cpython/commit/082875dcd6d482558e5f1da97a1c801d60b3ed5b
msg329701 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2018-11-12 00:24
New changeset aa493b5c18463ab45c087564e287643606f004ca by Julien Palard (Miss Islington (bot)) in branch '3.7':
bpo-33878: Doc: Fix missing case by simplifying. (GH-7762)
https://github.com/python/cpython/commit/aa493b5c18463ab45c087564e287643606f004ca
msg329702 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2018-11-12 00:24
New changeset d0ebbf4895439233c8398dbdd0456d07132b2a6f by Julien Palard (Miss Islington (bot)) in branch '3.6':
bpo-33878: Doc: Fix missing case by simplifying. (GH-7762)
https://github.com/python/cpython/commit/d0ebbf4895439233c8398dbdd0456d07132b2a6f
History
Date User Action Args
2018-11-12 00:37:25mdksetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-11-12 00:24:44mdksetmessages: + msg329702
2018-11-12 00:24:33mdksetmessages: + msg329701
2018-11-11 23:59:59miss-islingtonsetpull_requests: + pull_request9744
2018-11-11 23:59:50miss-islingtonsetpull_requests: + pull_request9743
2018-11-11 23:59:42mdksetmessages: + msg329697
2018-11-11 01:27:38emilyemorehousesetnosy: + emilyemorehouse
2018-11-10 04:15:13pablogsalsetpull_requests: + pull_request9719
2018-11-09 20:13:07gvanrossumsetmessages: + msg329554
2018-11-09 12:27:25serhiy.storchakasetnosy: + serhiy.storchaka, gvanrossum
messages: + msg329511
2018-06-17 10:47:16mdksetkeywords: + patch
stage: patch review
pull_requests: + pull_request7370
2018-06-16 23:52:17martin.pantersetmessages: + msg319797
2018-06-16 14:52:59mdksetmessages: + msg319743
2018-06-16 13:32:49martin.pantersetmessages: + msg319737
2018-06-16 13:01:46mdkcreate