Skip to content

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

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

Closed
JelleZijlstra opened this issue May 11, 2023 · 5 comments
Closed

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

JelleZijlstra opened this issue May 11, 2023 · 5 comments
Labels
release-blocker type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented May 11, 2023

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

@JelleZijlstra JelleZijlstra added type-bug An unexpected behavior, bug, or error release-blocker labels May 11, 2023
@JelleZijlstra JelleZijlstra changed the title List comprehension now have access to the enclosing class scope List comprehensions now have access to the enclosing class scope May 11, 2023
@asishm-wk
Copy link

asishm-wk commented May 11, 2023

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

@JelleZijlstra
Copy link
Member Author

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.

@asishm-wk
Copy link

asishm-wk commented May 11, 2023

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!

@carljm
Copy link
Member

carljm commented May 11, 2023

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

@carljm
Copy link
Member

carljm commented May 13, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-blocker type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

3 participants