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

Allow unparenthesized walrus in set comprehensions #86547

Closed
lysnikolaou opened this issue Nov 16, 2020 · 13 comments
Closed

Allow unparenthesized walrus in set comprehensions #86547

lysnikolaou opened this issue Nov 16, 2020 · 13 comments
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@lysnikolaou
Copy link
Contributor

BPO 42381
Nosy @gvanrossum, @lysnikolaou, @pablogsal, @miss-islington
PRs
  • bpo-42381: Allow walrus in set literals and set comprehensions #23332
  • [3.9] bpo-42381: Allow walrus in set literals and set comprehensions (GH-23332) #23333
  • bpo-42381: Document walrus-related syntax changes in whatsnew #23382
  • 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 = <Date 2020-11-18.23:50:58.954>
    created_at = <Date 2020-11-16.22:42:21.215>
    labels = ['interpreter-core', 'type-bug', '3.10']
    title = 'Allow unparenthesized walrus in set comprehensions'
    updated_at = <Date 2020-11-18.23:50:58.953>
    user = 'https://github.com/lysnikolaou'

    bugs.python.org fields:

    activity = <Date 2020-11-18.23:50:58.953>
    actor = 'lys.nikolaou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-18.23:50:58.954>
    closer = 'lys.nikolaou'
    components = ['Interpreter Core']
    creation = <Date 2020-11-16.22:42:21.215>
    creator = 'lys.nikolaou'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42381
    keywords = ['patch']
    message_count = 13.0
    messages = ['381173', '381177', '381187', '381188', '381190', '381191', '381192', '381194', '381195', '381201', '381203', '381383', '381384']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'lys.nikolaou', 'pablogsal', 'miss-islington']
    pr_nums = ['23332', '23333', '23382']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42381'
    versions = ['Python 3.10']

    @lysnikolaou
    Copy link
    Contributor Author

    Since unparenthesized walruses are allowed in list comprehensions and in generator expressions, should we maybe allow them in set comprehensions as well?

    @lysnikolaou lysnikolaou added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 16, 2020
    @gvanrossum
    Copy link
    Member

    Yeah, I suppose the comprehensions should all have the same syntax inside their various brackets.

    @pablogsal
    Copy link
    Member

    Apparently we currently don't allow walruses in set literals either:

    >>> {y := 4, 4**2, 3**3}
      File "<stdin>", line 1
        {y := 4, 4**2, 3**3}
           ^
    SyntaxError: invalid syntax

    But they should be allowed as well per PEP-572 (as the pep mentions all comprehensions):

    There is one special case: an assignment expression occurring in a list, set or dict comprehension or in a generator expression (below collectively referred to as "comprehensions") binds the target in ...

    @pablogsal
    Copy link
    Member

    The only question left is that if we should allow this on dict literals and dict comprehensions as well (because these constructs have a ":" in the middle that can be noisy if there are walrus, the answer isn't immediately obvious to me).

    @lysnikolaou
    Copy link
    Contributor Author

    dict comprehension

    FWIW, unparenthesized walruses are still disallowed in dictcomps and to be honest, I'm not sure if we should allow them or not. I feel that allowing too many colons there would be too noisy.

    {a:='a': b:=1} doesn't really look too beautiful.

    @gvanrossum
    Copy link
    Member

    Not in anything dict please.

    Nagging question — is there sufficient difference between {x := y} and {x: y} ?

    @lysnikolaou
    Copy link
    Contributor Author

    While {(a:='a'): (b:=1)} looks somewhat cleaner to me.

    @pablogsal
    Copy link
    Member

    Nagging question — is there sufficient difference between {x := y} and {x: y} ?

    I think there is enough distinction, yes. It will also be a bit odd if is allowed in list and list comps but not set and set comps

    @gvanrossum
    Copy link
    Member

    Okay.

    @pablogsal
    Copy link
    Member

    New changeset b0aba1f by Pablo Galindo in branch 'master':
    bpo-42381: Allow walrus in set literals and set comprehensions (GH-23332)
    b0aba1f

    @gvanrossum
    Copy link
    Member

    Let’s make sure this is all written up in whatsnew.

    @pablogsal
    Copy link
    Member

    New changeset 87c87b5 by Pablo Galindo in branch '3.9':
    [3.9] bpo-42381: Allow walrus in set literals and set comprehensions (GH-23332) (GH-23333)
    87c87b5

    @miss-islington
    Copy link
    Contributor

    New changeset a85fefe by Lysandros Nikolaou in branch 'master':
    bpo-42381: Document walrus-related syntax changes in whatsnew (GH-23382)
    a85fefe

    @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 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants