-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Walrus comprehension rebind checking behavior #87447
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
Comments
# test PEP-572 disallows walrus use cases such as >>> [(a := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'a'
>>> [(b := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'b'
>>> [(c := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'c'
>>> [(d := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'd'
>>> [(e := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'e'
>>> [(f := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'f'
>>> [(g := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'g'
>>> [(h := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'h'
>>> [(i := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
TypeError: cannot unpack non-iterable int object Among all of these assignment expressions, only the assignment to The same behavior is also observed when detecting inner loop variable rebinding outer loop assignment expression targets: >>> [i for i in range(5) if (j := 0) for k[j + 1] in range(5)]
File "<stdin>", line 1
SyntaxError: comprehension inner loop cannot rebind assignment expression target 'j' |
I'd like Emily to have a look at that. |
I had some free time to learn more about |
I think it probably needs a little more discussion whether this should be fixed. I found it useful to look at a slightly simpler example than the test case, like: index = 0
a = [0, 0, 0]
[(index := i) for i, a[index] in enumerate([9,8,7])] Arguments in favour of changing:
Arguments against changing:
Overall, I think I lean against, but I could be convinced otherwise! |
My main argument to fix this is that error message does not show the reality: >>> [(h := 1) for a, (*b, c[d+e::f(g)], h.i) in [1]]
File "<stdin>", line 1
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'h' But, So, in my opinion it is clearly a bug. But, there are two ways of fixing it:
|
I feel for the OP and I agree with several arguments in favor of this fix, given that it's a simple fix. Shantanu's arguments against feel pretty weak (probably intentionally so: "at least a little", "maybe a little"). Maybe the other thing (disallowing named expressions in the iterable altogether) can be fixed in another PR? |
Are you talking about forbidding # Compute partial sums in a list comprehension
total = 0
partial_sums = [total := total + v for v in values]
print("Total:", total) However, I probably just didn't get your idea :) |
I was talking about this from Shantanu:
This is about things like: >>> [x for x in (a := range(3))]
File "<stdin>", line 1
SyntaxError: assignment expression cannot be used in a comprehension iterable expression
>>> |
Co-authored-by: Pablo Galindo Salgado <[email protected]> Co-authored-by: Shantanu <[email protected]>
Fixed in #100581 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: