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

PEP 572 : assignment expression to a global variable in a comprehension #82650

Closed
PierreQuentel mannequin opened this issue Oct 13, 2019 · 10 comments
Closed

PEP 572 : assignment expression to a global variable in a comprehension #82650

PierreQuentel mannequin opened this issue Oct 13, 2019 · 10 comments
Labels
3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@PierreQuentel
Copy link
Mannequin

PierreQuentel mannequin commented Oct 13, 2019

BPO 38469
Nosy @gvanrossum, @ncoghlan, @ambv, @PierreQuentel, @emilyemorehouse, @pablogsal
PRs
  • bpo-38469: Handle named expression scope whith global/nonlocal keywords #16755
  • [3.8] bpo-38469: Handle named expression scope with global/nonlocal keywords (GH-16755) #16760
  • 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 2019-10-14.18:34:35.368>
    created_at = <Date 2019-10-13.20:54:11.116>
    labels = ['interpreter-core', 'type-bug', '3.8', '3.9']
    title = 'PEP 572 : assignment expression to a global variable in a comprehension'
    updated_at = <Date 2019-10-14.21:42:18.909>
    user = 'https://github.com/PierreQuentel'

    bugs.python.org fields:

    activity = <Date 2019-10-14.21:42:18.909>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-14.18:34:35.368>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2019-10-13.20:54:11.116>
    creator = 'quentel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38469
    keywords = ['patch']
    message_count = 10.0
    messages = ['354601', '354603', '354604', '354606', '354607', '354608', '354615', '354628', '354644', '354664']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'ncoghlan', 'lukasz.langa', 'quentel', 'emilyemorehouse', 'pablogsal']
    pr_nums = ['16755', '16760']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38469'
    versions = ['Python 3.8', 'Python 3.9']

    @PierreQuentel
    Copy link
    Mannequin Author

    PierreQuentel mannequin commented Oct 13, 2019

    PEP-572 says that "an assignment expression occurring in a (...) comprehension (...) binds the target in the containing scope, honoring a nonlocal or global declaration for the target in that scope, if one exists."

    In Appendix B, the PEP shows this example :

    def f():
        global TARGET
        a = [TARGET := EXPR for VAR in ITERABLE]

    So I don't understand why this fails:

    Python 3.8.0rc1 (tags/v3.8.0rc1:34214de, Oct  1 2019, 18:42:37) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> x = 0
    >>> def f():
    ...     global x
    ...     [x := i for i in range(5)]
    ...
      File "<stdin>", line 3
    SyntaxError: no binding for nonlocal 'x' found
    >>>

    Is this a bug or am I missing something ?

    @PierreQuentel PierreQuentel mannequin added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 13, 2019
    @pablogsal
    Copy link
    Member

    I think this is a bug in the symtable_extend_namedexpr_scope function because when visiting the stack in reverse order and finding a function block is always assigning the scope as DEF_NONLOCAL.

    Marking also as a release blocker unless Nick, Guido or Emily confirm otherwise.

    @pablogsal pablogsal added release-blocker 3.9 only security fixes labels Oct 13, 2019
    @gvanrossum
    Copy link
    Member

    It's a bug, but it's honestly such an odd corner case that I don't think this should hold up the 3.8.0 release. Let's fix it in 3.9 and backport it to 3.8.1.

    @pablogsal
    Copy link
    Member

    I am working on a fix now, se we can merge it before the final release

    @gvanrossum
    Copy link
    Member

    Thanks for working on a fix. But is it really important enough to violate the desired "rc1 == release" invariant? It's up to the release manager.

    @pablogsal
    Copy link
    Member

    But is it really important enough to violate the desired "rc1 == release" invariant?

    I created the fix because many other bug fixes have been merged since rc1 already so apologies if it seemed a bit precipitate. Let's see what the release manager thinks (in case the bugfix is correct) :)

    @pablogsal
    Copy link
    Member

    New changeset fd5c414 by Pablo Galindo in branch 'master':
    bpo-38469: Handle named expression scope with global/nonlocal keywords (GH-16755)
    fd5c414

    @ambv
    Copy link
    Contributor

    ambv commented Oct 14, 2019

    New changeset 927f07c by Łukasz Langa (Miss Islington (bot)) in branch '3.8':
    bpo-38469: Handle named expression scope with global/nonlocal keywords (GH-16755) (bpo-16760)
    927f07c

    @PierreQuentel
    Copy link
    Mannequin Author

    PierreQuentel mannequin commented Oct 14, 2019

    That was a quick fix, thanks !

    @ambv
    Copy link
    Contributor

    ambv commented Oct 14, 2019

    (FTR the fix is part of Python 3.8.0)

    @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.8 only security fixes 3.9 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

    3 participants