-
-
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
if type(var) is class: ... doesn't constrain type as expected #5847
Comments
Note that mypy doesn't have a way (internally, nor in the type system) to express "B but not a subclass of B". The best we could probably do is make Until this is fixed, as a workaround, you could write |
Thank you. Other variant, use Or, may be, it will be better to implement some special type of comment like |
The cast works, but I don't like it, because it silences possible errors too. Adding type comments is not recommended. We should probably just add support for |
I've been adding mypy to my work code base and this is one of the problems I've encountered. (The other one is unions being used in a runtime context error that should be reverted in my opinion.) This is the pattern I see most often:
I agree it's not high priority, but I do see it in use. Thanks for the workaround :) |
This appears to work fine in the latest version of mypy, so I think it can be closed. |
Agreed. |
Consider simplified code (mypy 0.641 messages are in comments):
Both loops are equivalent, but mypy complains about line 29 because the "isinstance(x, B)" works but "type(x) is B" doesn't. Situation is slightly similar to the issue #4864.
P.S. In real situation I have more complicated hierarchy of classes (including classes C(B) etc.) and need to constraint processing to objects of type B only.
The text was updated successfully, but these errors were encountered: