8000 gh-104602: ensure all cellvars are known up front by carljm · Pull Request #104603 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104602: ensure all cellvars are known up front #104603

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

Merged
merged 5 commits into from
May 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into nestedcomps
  • Loading branch information
JelleZijlstra authored May 18, 2023
commit a3826234bd4887cea8caabfd1b7f0188cbdd1fdd
103 changes: 103 additions & 0 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,109 @@ def f():
self._check_in_scopes(
code, {"x": 2}, ns={"b": 2}, scopes=["function", "module"])

def test_name_error_in_class_scope(self):
code = """
y = 1
[x + y for x in range(2)]
"""
self._check_in_scopes(code, raises=NameError, scopes=["class"])

def test_global_in_class_scope(self):
code = """
y = 2
vals = [(x, y) for x in range(2)]
"""
outputs = {"vals": [(0, 1), (1, 1)]}
self._check_in_scopes(code, outputs, ns={"y": 1}, scopes=["class"])

def test_in_class_scope_inside_function_1(self):
code = """
class C:
y = 2
vals = [(x, y) for x in range(2)]
vals = C.vals
"""
outputs = {"vals": [(0, 1), (1, 1)]}
self._check_in_scopes(code, outputs, ns={"y": 1}, scopes=["function"])

def test_in_class_scope_inside_function_2(self):
code = """
y = 1
class C:
y = 2
vals = [(x, y) for x in range(2)]
vals = C.vals
"""
outputs = {"vals": [(0, 1), (1, 1)]}
self._check_in_scopes(code, outputs, scopes=["function"])

def test_in_class_scope_with_global(self):
code = """
y = 1
class C:
global y
y = 2
# Ensure the listcomp uses the global, not the value in the
# class namespace
locals()['y'] = 3
vals = [(x, y) for x in range(2)]
vals = C.vals
"""
outputs = {"vals": [(0, 2), (1, 2)]}
self._check_in_scopes(code, outputs, scopes=["module", "class"])
outputs = {"vals": [(0, 1), (1, 1)]}
self._check_in_scopes(code, outputs, scopes=["function"])

def test_in_class_scope_with_nonlocal(self):
code = """
y = 1
class C:
nonlocal y
y = 2
# Ensure the listcomp uses the global, not the value in the
# class namespace
locals()['y'] = 3
vals = [(x, y) for x in range(2)]
vals = C.vals
"""
outputs = {"vals": [(0, 2), (1, 2)]}
self._check_in_scopes(code, outputs, scopes=["function"])

def test_nested_has_free_var(self):
code = """
items = [a for a in [1] if [a for _ in [0]]]
"""
outputs = {"items": [1]}
self._check_in_scopes(code, outputs, scopes=["class"])

def test_nested_free_var_not_bound_in_outer_comp(self):
code = """
z = 1
items = [a for a in [1] if [x for x in [1] if z]]
"""
self._check_in_scopes(code, {"items": [1]}, scopes=["module", "function"])
self._check_in_scopes(code, {"items": []}, ns={"z": 0}, scopes=["class"])

def test_nested_free_var_in_iter(self):
code = """
items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]]
"""
self._check_in_scopes(code, {"items": [1]})

def test_nested_free_var_in_expr(self):
code = """
items = [(_C, [x for x in [1] if _C]) for _C in [0, 1]]
"""
self._check_in_scopes(code, {"items": [(0, []), (1, [1])]})

def test_nested_listcomp_in_lambda(self):
code = """
f = [(z, lambda y: [(x, y, z) for x in [3]]) for z in [1]]
(z, func), = f
out = func(2)
"""
self._check_in_scopes(code, {"z": 1, "out": [(3, 2, 1)]})


__test__ = {'doctests' : doctests}

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0