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
PEP 572 Walrus Operator Regex Assignment in Condition #8447
#6899 implements PEP 572, or at least parts of it, as mentioned in the PR assignments can occur anywhere. Fails with an example similar to here with regex.
Minimal Example:
importreTXT="b"if (a:=re.search(r"a", TXT)) isnotNone:
string=a.stringprint(f"{string}")
else:
print("not in there") #=> not in there
mypy message:
~ λ mypy test.py
test.py:8: error: Item "None" of "Optional[Match[str]]" has no attribute "string"
Found 1 error in 1 file (checked 1 source file)
but the type of a is not Optional[Match[str]] it's Match[str] as a result of the if check.
Activity
JukkaL commentedon Feb 28, 2020
Yeah, type inference doesn't seem to work correctly here.
As a workaround, you can remove the
is not Nonecheck, and things should work as expected (when using GitHub master, at least):Support narrowing of walrus in most cases
hauntsaninja commentedon Feb 28, 2020
Same issue as in #8236
Support narrowing of walrus in most cases (#8458)
Support narrowing of walrus in most cases (#8458)