-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Incompatibles types in assignment #5765
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
This is not a bug in mypy. Your issue is that |
Same misunderstanding appeared previously in #4186 (and in other issues). I am raising priority of that issue to high (also there is a simple solution, add the same note with the link to invariance that we add for bad argument type). |
gvanrossum / ilevkivskyi, thank you for your attention on this. I researched the suggested topics worked up a couple more examples and at first I couldn't see the problem. I decided to document some of my own thought process on this in case it helps others. In my example, the problem is that GetParams() could save a reference to the List it returns for later manipulation. It would assume (rightly) that the List could ever only contain Variant objects, but params has a Union and so could violate the type of the saved copy of the list inside GetParams(). Presumably then, the fix in my case would be to make a copy of the List returned, but this still fails (see code example below). So I did even more research and now I think I understand the problem. While this code is technically correct in it's current state, it fails the "Liskov substitution code smell test" and that boils down to "there's probably a cleaner way to implement this". Right after I posted this issue, in the interest of moving past it, I did end up finding a cleaner solution which essentially involved params below no longer needing to expect to have to contain a VALUE and so the type definition of params became List[Variant] and mypy is now happy and I now don't have to do instanceof checks on it's values. tldr; you guys are awesome, thanks!!!!
|
Yes, mypy isn't smart enough to figure out that making a copy is safe. This can probably be solved by giving |
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Do you see the same issue after installing mypy from Git master?
The text was updated successfully, but these errors were encountered: