-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Non-overlapping identity check when member variable mutated by function call #9005
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
Comments
Yeah, this behavior is confusing. #9021 fixed a similar issue with booleans. A possible fix would be to omit non-overlapping equality checks based on enum values, since they are likely to result in false positives. |
What does this have to do with enums, specifically? It seems to me that values of any type could run into this; the real problem is that making a method call ought to invalidate prior knowledge of the value. |
@Hawk777 it's a tradeoff, but my sense is that doing so would invalidate the precise type too often, which would then lead to annoying false positives. |
Yeah, this is all based on heuristics. There's no way we can reliably decide whether a function or method (or operator) could modify some inferred type as a side effect, so we make the simplification of assuming that it doesn't happen. This works well in practice, except for certain idioms which tend to generate false positives. |
One work-around is using no-op assignments whenever there are side-effects, to make mypy forget its learned constraints: assert foo.status == 'spam'
foo.refresh_from_db()
foo = foo # <-- Work-around for https://github.com/python/mypy/issues/9005
assert foo.status == 'eggs'
# ^^^^^^ mypy no longer complains
Maybe creating an annotation with PEP 593 E.g. class Widget:
def refresh_from_db(self: Annotated[Widget, mypy_extensions.Mutates]) -> None:
self.status = ... Then every time |
I get the following error in a situation where I think I shouldn’t:
Given the following code:
If I uncomment the commented line, the error goes away. It seems that Mypy does not understand that a method call might mutate member variables of the class, obsoleting any prior checks of their values.
This is when running version 0.780 with
--strict
.The text was updated successfully, but these errors were encountered: