You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Walrus operators unrecognised in dictionary initialisation of a class attribute #8379
It seems Walrus operators (in Python 3.8+) are not recognised by mypy when used in initialising a dictionary of a class attribute. Here is a test code.
This will also happen with a list e.g. l = [x2 := 1, x2 + 2, x2 + 3] (for this case also it will only happen if it is in the class scope).
I have noticed, however, that it only happens on RE-USE of the variable from the assignment expression within the initialisation. Otherwise the walrus operator is ok.
class Example:
# l = [x2 := 1, x2 + 2, x2 + 3] # not OK
l = [x2 := 1, 2, 3] # OK
Activity
dhood commentedon Jul 5, 2020
This will also happen with a list e.g.
l = [x2 := 1, x2 + 2, x2 + 3](for this case also it will only happen if it is in the class scope).I have noticed, however, that it only happens on RE-USE of the variable from the assignment expression within the initialisation. Otherwise the walrus operator is ok.
bunny-therapist commentedon Aug 19, 2023
I just (2023) ran into this exact problem with
it176131 commentedon May 28, 2024
Any proposed fix—besides
# type: ignore[name-defined]—for this? I ran into it with the following versions:JelleZijlstra commentedon May 28, 2024
Anyone impacted should feel free to work on a PR to fix this. I haven't looked at the code but I imagine it's a relatively easy fix.