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

Closed
Brando753 mannequin opened this issue Nov 10, 2020 · 4 comments
Closed

Walrus Operator in list index #86482

Brando753 mannequin opened this issue Nov 10, 2020 · 4 comments
Assignees
Labels
3.10 only security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@Brando753
Copy link
Mannequin

Brando753 mannequin commented Nov 10, 2020

BPO 42316
Nosy @terryjreedy, @lysnikolaou
PRs
  • gh-86482: Document assignment expression need for ()s #23291
  • bpo-42316: Allow unparenthesized walrus operator in indexes #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 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.

    @Brando753 Brando753 mannequin added topic-asyncio 3.8 only security fixes labels Nov 10, 2020
    @terryjreedy
    Copy link
    Member

    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 Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes and removed topic-asyncio 3.8 only security fixes labels Nov 14, 2020
    @terryjreedy terryjreedy added docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes and removed topic-asyncio 3.8 only security fixes labels Nov 14, 2020
    @lysnikolaou
    Copy link
    Contributor

    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
    JelleZijlstra added a commit that referenced this issue Oct 6, 2022
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 6, 2022
    …-23291)
    
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    (cherry picked from commit 2b5f136)
    
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 6, 2022
    …-23291)
    
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    (cherry picked from commit 2b5f136)
    
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    @JelleZijlstra JelleZijlstra self-assigned this Oct 6, 2022
    miss-islington added a commit that referenced this issue Oct 6, 2022
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    (cherry picked from commit 2b5f136)
    
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    miss-islington added a commit that referenced this issue Oct 6, 2022
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    (cherry picked from commit 2b5f136)
    
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    carljm added a commit to carljm/cpython that referenced this issue Oct 6, 2022
    * main:
      fixes pythongh-96078: os.sched_yield release the GIL while calling sched_yield(2). (pythongh-97965)
      pythongh-65961: Do not rely solely on `__cached__` (pythonGH-97990)
      pythongh-97850: Remove the open issues section from the import reference (python#97935)
      Docs: pin sphinx-lint (pythonGH-97992)
      pythongh-94590: add signatures to operator itemgetter, attrgetter, methodcaller (python#94591)
      Add Pynche's move to the What's new in 3.11 (python#97974)
      pythongh-97781: Apply changes from importlib_metadata 5. (pythonGH-97785)
      pythongh-86482: Document assignment expression need for ()s (python#23291)
      pythongh-97943: PyFunction_GetAnnotations should return a borrowed reference. (python#97949)
      pythongh-94808: Coverage: Test that maximum indentation level is handled (python#95926)
    carljm added a commit to carljm/cpython that referenced this issue Oct 8, 2022
    * main:
      pythonGH-97002: Prevent `_PyInterpreterFrame`s from backing more than one `PyFrameObject` (pythonGH-97996)
      pythongh-97973: Return all necessary information from the tokenizer (pythonGH-97984)
      fixes pythongh-96078: os.sched_yield release the GIL while calling sched_yield(2). (pythongh-97965)
      pythongh-65961: Do not rely solely on `__cached__` (pythonGH-97990)
      pythongh-97850: Remove the open issues section from the import reference (python#97935)
      Docs: pin sphinx-lint (pythonGH-97992)
      pythongh-94590: add signatures to operator itemgetter, attrgetter, methodcaller (python#94591)
      Add Pynche's move to the What's new in 3.11 (python#97974)
      pythongh-97781: Apply changes from importlib_metadata 5. (pythonGH-97785)
      pythongh-86482: Document assignment expression need for ()s (python#23291)
      pythongh-97943: PyFunction_GetAnnotations should return a borrowed reference. (python#97949)
    mpage pushed a commit to mpage/cpython that referenced this issue Oct 11, 2022
    …3291)
    
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    pablogsal pushed a commit that referenced this issue Oct 22, 2022
    Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
    (cherry picked from commit 2b5f136)
    
    Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
    @hauntsaninja
    Copy link
    Contributor

    Thanks, looks like we've completed this

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants