gh-118513: Fix sibling comprehensions with a name bound in one and global in the other (#118526)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
This commit is contained in:
Carl Meyer 2024-05-03 08:05:19 -06:00 committed by GitHub
parent 37ccf16786
commit c8deb1e4b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 39 deletions

View file

@ -666,6 +666,20 @@ class ListComprehensionTest(unittest.TestCase):
self._check_in_scopes(code, expected)
self._check_in_scopes(code, expected, exec_func=self._replacing_exec)
def test_multiple_comprehension_name_reuse(self):
code = """
[x for x in [1]]
y = [x for _ in [1]]
"""
self._check_in_scopes(code, {"y": [3]}, ns={"x": 3})
code = """
x = 2
[x for x in [1]]
y = [x for _ in [1]]
"""
self._check_in_scopes(code, {"x": 2, "y": [3]}, ns={"x": 3}, scopes=["class"])
self._check_in_scopes(code, {"x": 2, "y": [2]}, ns={"x": 3}, scopes=["function", "module"])
__test__ = {'doctests' : doctests}