Visitar URL original
Walrus Operator in list index · Issue #86482 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20220512051219/https://github.com/python/cpython/issues/86482
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Walrus Operator in list index #86482

Open
Brando753 mannequin opened this issue Nov 10, 2020 · 3 comments
Open

Walrus Operator in list index #86482

Brando753 mannequin opened this issue Nov 10, 2020 · 3 comments
Labels
3.10 docs interpreter-core

Comments

@Brando753
Copy link
Mannequin

@Brando753 Brando753 mannequin commented Nov 10, 2020

BPO 42316
Nosy @terryjreedy, @lysnikolaou
PRs
  • #23291
  • #23317
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-11-10.17:28:35.243>
    labels = ['interpreter-core', '3.10', 'docs']
    title = 'Walrus Operator in list index'
    updated_at = <Date 2020-11-16.23:09:41.621>
    user = 'https://bugs.python.org/Brando753'

    bugs.python.org fields:

    activity = <Date 2020-11-16.23:09:41.621>
    actor = 'lys.nikolaou'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'Interpreter Core']
    creation = <Date 2020-11-10.17:28:35.243>
    creator = 'Brando753'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42316
    keywords = ['patch']
    message_count = 3.0
    messages = ['380691', '380970', '381179']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'docs@python', 'lys.nikolaou', 'Brando753']
    pr_nums = ['23291', '23317']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42316'
    versions = ['Python 3.10']

    @Brando753
    Copy link
    Mannequin Author

    @Brando753 Brando753 mannequin commented Nov 10, 2020

    Reading the PEP-572 document I don't see anything stating that Walrus operator in list indexes must be enclosed in parenthesis.

    Minimal Example:
    '''
    In [1]: a = list(range(10))

    In [2]: idx = -1

    In [3]: a[idx := idx +1]
    File "<ipython-input-3-65fc931e77dd>", line 1
    a[idx := idx +1]
    ^
    SyntaxError: invalid syntax

    '''

    If you enclose in parenthesis it works as expected:
    '''
    In [4]: a[(idx := idx +1)]
    Out[4]: 0

    In [5]: a[(idx := idx +1)]
    Out[5]: 1

    In [6]: a[(idx := idx +1)]
    Out[6]: 2
    '''

    Is this a bug or am I misreading the PEP-572 document somewhere? It's not a top-level assignment nor is it a list comprehension so I would expect it to work in the first example.

    @terryjreedy
    Copy link
    Member

    @terryjreedy terryjreedy commented Nov 14, 2020

    PEP-572 does not saw much of anything about when parens are needed. Nor does the low priority itself. Looking through the grammar of expressions, an assignment_expression is also a starred_expression, a positional_item (in calls), and the first part of a comprehension. It is not itself an plain expression unless and until wrapped in parentheses. Subscription requires an expression list, which may be a single expression.

    To put it another way: an expression is an assignment expression but an assignment expression with a "name :=" prefix is not an expression, so parens are required anywhere an expression is required. That includes subscripts, slicings, displays, call items other than positional items, comprehension parts other than the initial expression (maybe, check), parts of conditional expressions (maybe, check), and lambda expression bodies. After checking, I would like a sentence added to the doc before the 'go see the PEP' bit.

    This has nothing to do with asyncio.

    @terryjreedy terryjreedy added docs interpreter-core 3.10 and removed expert-asyncio 3.8 labels Nov 14, 2020
    @terryjreedy terryjreedy added docs interpreter-core 3.10 and removed expert-asyncio 3.8 labels Nov 14, 2020
    @lysnikolaou
    Copy link
    Contributor

    @lysnikolaou lysnikolaou commented Nov 16, 2020

    New changeset cae6018 by Lysandros Nikolaou in branch 'master':
    bpo-42316: Allow unparenthesized walrus operator in indexes (GH-23317)
    cae6018

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 docs interpreter-core
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants