gh-119666: fix multiple class-scope comprehensions referencing __class__ (#120295)

This commit is contained in:
Carl Meyer 2024-06-09 22:23:30 -04:00 committed by GitHub
parent 34f5ae69fe
commit 0ae8579b85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 13 deletions

View file

@ -168,6 +168,31 @@ class ListComprehensionTest(unittest.TestCase):
"""
self._check_in_scopes(code, raises=NameError)
def test_references___class___defined(self):
code = """
__class__ = 2
res = [__class__ for x in [1]]
"""
self._check_in_scopes(
code, outputs={"res": [2]}, scopes=["module", "function"])
self._check_in_scopes(code, raises=NameError, scopes=["class"])
def test_references___class___enclosing(self):
code = """
__class__ = 2
class C:
res = [__class__ for x in [1]]
res = C.res
"""
self._check_in_scopes(code, raises=NameError)
def test_super_and_class_cell_in_sibling_comps(self):
code = """
[super for _ in [1]]
[__class__ for _ in [1]]
"""
self._check_in_scopes(code, raises=NameError)
def test_inner_cell_shadows_outer(self):
code = """
items = [(lambda: i) for i in range(5)]