Skip to content

PEP 572 Walrus Operator Regex Assignment in Condition #8447

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

Closed
NickHackman opened this issue Feb 27, 2020 · 2 comments · Fixed by #8458 or sthagen/python-mypy#32
Closed

PEP 572 Walrus Operator Regex Assignment in Condition #8447

NickHackman opened this issue Feb 27, 2020 · 2 comments · Fixed by #8458 or sthagen/python-mypy#32
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-strict-optional

Comments

@NickHackman
Copy link

Mypy version: 0.761
Python version: 3.8.1

#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:

import re


TXT = "b"
if (a := re.search(r"a", TXT)) is not None:
    string = a.string
    print(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.

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-strict-optional labels Feb 28, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 28, 2020

Yeah, type inference doesn't seem to work correctly here.

As a workaround, you can remove the is not None check, and things should work as expected (when using GitHub master, at least):

import re


TXT = "b"
if a := re.search(r"a", TXT):   # <<-- removed the None check
    string = a.string  # No error here
    print(f"{string}")
else:
    print("not in there")

msullivan added a commit that referenced this issue Feb 28, 2020
It is a pretty simple matter of pulling out the assignment target from
the walrus. We don't bother handling things like `x := (y := z)` since
I can't imagine they are common enough to be worth bothering but we
could in the future if anyone cares.

Fixes #8447.
@hauntsaninja
Copy link
Collaborator

Same issue as in #8236

msullivan added a commit that referenced this issue Feb 28, 2020
It is a pretty simple matter of pulling out the assignment target from
the walrus. We don't bother handling things like `x := (y := z)` since
I can't imagine they are common enough to be worth bothering but we
could in the future if anyone cares.

Fixes #8447.
msullivan added a commit that referenced this issue Mar 9, 2020
It is a pretty simple matter of pulling out the assignment target from
the walrus. We don't bother handling things like `x := (y := z)` since
I can't imagine they are common enough to be worth bothering but we
could in the future if anyone cares.

Fixes #8447.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-strict-optional
Projects
None yet
3 participants