-
-
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
--warn-unreachable false positive with minimal example without types explicitly set #7204
Comments
I encountered something similar in our codebase. I agree that it's a bug. |
This actually may be not easy to fix. The problem here is how binder interacts with partial types. We have this nice thing called conditional type binder that does our version of abstract interpretation (thanks to Pyre team for the official term for this). In particular, it repeatedly checks loop bodies until no types have changed. Here is a problem however: it looks like it doesn't detect a change of type if it happens because of partial types (partial We have an item in our roadmap to refactor/rethink the binder. Maybe we should actually do this, maybe this should be the next big refactoring after we are done with semantic analyzer? My ideal large scale view of this is like we have fine grained targets (module top-levels and top level functions). These targets can be deferred (currently only functions), but unlike in semantic analyzer we process them in mixed order because some attributes inferred in constructor may be needed at a top level. In every target we have an "abstract interpreter" that interprets expressions in domain of types instead of domain of values. It combines the functionality currently divided between partial types and type binder. It seems to me such model may be also more reasonable if we are going to move A downside however is that we might need to make |
I think that we should first try to come up with a simple fix. I suspect that we don't need a big redesign to fix this, but I'm not sure. @ilevkivskyi Can you create a separate issue about the binder refactoring? I agree that it's a good candidate for our next big refactoring. The first step might be a pure refactor that just cleans up and documents the existing behavior. |
Another testcase. This one causes
|
I have the same scenario described by @Herst, it looks indeed a bug. |
Hello,
I just downloaded the new mypy v0.720 and am playing with the
--warn-unreachable-flag
in my projects.I noticed one occasion where there is something that seems like a false positive, so I am making an issue for it with a minimal reproducible example.
Version
source code example
This when tested with
mypy test.py --warn-unreachable
returns:So essentially telling us that
if found_id
is never entered due to thefound_id
type beingNone
. But in reality the type should beOptional[int]
since you can see that it's set toentry['id']
later.If you explicitly set the types mypy no longer complains:
Bug or not
Essentially I am not sure if this is intended behaviour or not. After writing this issue down I realize it all boils down to the matter of mypy not correctly inferring type of
found_id
.The text was updated successfully, but these errors were encountered: