-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
False positive unreachable
when variable is set and reset inside loop
#14987
Labels
bug
mypy got something wrong
Comments
Similar problem here while using mypy==1.3.0. My example code: data = """
{foo}
bar >
baz
"""
prompt = None
reponse = None
lines = data.splitlines()
for line in lines:
if prompt and reponse: # mypy error: Right operand of "and" is never evaluated [unreachable]
print("Break reached!") # mypy error: Statement is unreachable [unreachable]
break
if not prompt and line.endswith(">"):
prompt = line
continue
if not reponse and line.endswith("}"):
reponse = line
continue Run output:
|
+1 Here is a smaller example: state = None
for x in 'ab':
if state is not None:
print('reachable')
state = 'x' The script output:
Mypy error: main.py:4:9: error: Statement is unreachable [unreachable]
print('reachable')
^~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)
✕ mypy failed. |
Nice, thanks for the ping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
mypy warns about the right side of a condition being unreachable when a variable is reset in an outer loop, then set again inside the inner loop.
To Reproduce
Expected Behavior
No error.
Actual Behavior
mypy outputs
error: Right operand of "or" is never evaluated
. This is clearly because the type oftest
is re-evaluated at some point, even though it was declared with a type and no statement narrows that type.This appears related to #7204, #8721, #8865, #13973, #14120, except it can't be solved by declaring the type of the variable before the loop in which it's updated (because that violates
no-redef
).Note: I'm not sure why anyone would want to actually do this in practice. But I thought it might provide a useful test case.
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: