New submission from Nick Coghlan <ncogh...@gmail.com>:
While implementing PEP 572, Emily noted that the check for conflicts between assignment operators and comprehension iteration variables had not yet been implemented: https://bugs.python.org/issue35224#msg334331 Damien George came across this discrepancy while implementing assignment expressions for MicroPython. The proposed discussion regarding whether or not the PEP should be changed didn't happen, and the PEP itself misses the genuinely confusing cases where even an assignment expression that *never executes* will still make the iteration variable leak: >>> [i for i in range(5)] [0, 1, 2, 3, 4] >>> [i := 10 for i in range(5)] [10, 10, 10, 10, 10] >>> i 10 >>> [False and (i := 10) for i in range(5)] [False, False, False, False, False] >>> i 4 And that side effect happens even if the assignment expression is nested further down in an inner loop: >>> [(i, j, k) for i in range(2) for j in range(2) for k in range(2)] [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] >>> i Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'i' is not defined >>> [(i, j, k) for i in range(2) for j in range(2) for k in range(2) if True or >>> (i:=10)] [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] >>> i 1 I'm at the PyCon AU sprints today, and will be working on a PR to make these cases raise TargetScopeError as specified in the PEP. ---------- assignee: ncoghlan messages: 349012 nosy: Damien George, emilyemorehouse, ncoghlan priority: deferred blocker severity: normal stage: needs patch status: open title: TargetScopeError not raised for comprehension scope conflict type: behavior versions: Python 3.8, Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue37757> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com