GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — List comprehensions now have access to the enclosing class scope · Issue #104374 · python/cpython

List comprehensions now have access to the enclosing class scope #104374

New issue
Copy link
New issue
Copy link
Closed
Closed
List comprehensions now have access to the enclosing class scope#104374
Copy link
Labels
release-blockertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@JelleZijlstra
JelleZijlstra
opened on May 11, 2023
Issue body actions

Code:

class X:
    y = 1
    print([x + y for x in range(3)])

On current main:

Python 3.12.0a7+ (heads/main:94f30c7557, May 10 2023, 17:27:39) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...     y = 1
...     print([x + y for x in range(3)])
... 
[1, 2, 3]

But on 3.11:

>>> class X:
...     y = 1
...     print([x + y for x in range(3)])
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in X
  File "<stdin>", line 3, in <listcomp>
NameError: name 'y' is not defined

I actually like the new behavior, and I discovered this bug as I was preparing a post proposing that we make things work this way in 3.13. But it's a behavior change, and we should only make this change on purpose and not by accident.

This could plausibly break real code if the name is defined in both the module and class scope, and current code relies on getting the name from the module scope.

cc @carljm

Linked PRs

  • gh-104374: don't inline comprehensions in non-function scopes #104519
  • gh-104374: Remove access to class scopes for inlined comprehensions #104528

Activity

  1. JelleZijlstra
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    release-blocker
    on May 11, 2023
  2. ezio-melotti
    added this to Release and Deferred blockers 🚫on May 11, 2023
  3. github-project-automation
    moved this to Todo in Release and Deferred blockers 🚫on May 11, 2023
  4. JelleZijlstra
    changed the title [-]List comprehension now have access to the enclosing class scope[/-] [+]List comprehensions now have access to the enclosing class scope[/+] on May 11, 2023
  5. asishm-wk commented on May 11, 2023

    @asishm-wk
    asishm-wk
    on May 11, 2023
    More actions

    on 3.10.8 - this prints [1,2,3]

  6. JelleZijlstra commented on May 11, 2023

    @JelleZijlstra
    JelleZijlstra
    on May 11, 2023
    MemberAuthor
    More actions

    I don't think so. I just confirmed on 3.10.9 (don't have a 3.10.8 around), and it throws a NameError. Make sure you run the sample in a class scope.

  7. asishm-wk commented on May 11, 2023

    @asishm-wk
    asishm-wk
    on May 11, 2023
    More actions

    sorry, you're right! - I forgot to restart the interpreter when testing different variations - so there was already a y defined in the outer scope!

  8. carljm commented on May 11, 2023

    @carljm
    carljm
    on May 11, 2023
    Member
    More actions

    I started a Discourse thread about options for handling this: https://discuss.python.org/t/pep-709-one-behavior-change-that-was-missed-in-the-pep/26691

  9. carljm commented on May 13, 2023

    @carljm
    carljm
    on May 13, 2023
    Member
    More actions

    I filed a SC issue about this: python/steering-council#189

  10. carljm
    added a commit that references this issue on May 16, 2023

    pythongh-104374: don't inline comprehensions in non-function scopes

    9d452fb
  11. JelleZijlstra
    added 2 commits that reference this issue on May 16, 2023

    pythongh-104374: Remove access to class scopes for inlined comprehens…

    3fad7b0

    gh-104374: Remove access to class scopes for inlined comprehensions (#…

    662aede
  12. carljm
    closed this as completedon May 18, 2023
  13. github-project-automation
    moved this from Todo to Done in Release and Deferred blockers 🚫on May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    release-blockertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Release and Deferred blockers 🚫
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.