-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
report error for false type assertions #4306
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
Note that this is somewhat in conflict with #3603. Right now, mypy gives the empty type to |
@elazarg I don't completely follow why Would this be possible to check correctly: i : int = 0
assert isinstance(i, str) # error |
It is not the In your specific example, it is obviously possible to check correctly since you know def f(i: int) -> None:
assert isinstance(i, str) # error Currently mypy will assign |
I see, so for some subclass of Is this also the case for returned types? e.g.: def foo() -> int:
return 0
def test_foo():
a = foo()
assert isinstance(a, str) If no, then the feature request -- generating an error when type analysis tells us the assert is false -- would still have validity. |
Mypy will consider the latter type "inferred", but otherwise it is the same. However, you can try a: str = foo() And mypy will complain, if that's what you want. This is a kind of static assertion, as opposed to the dynamic assertion in your examples. I think this is closer to what you actually looking for. |
This is a duplicate of #2395 |
There is now |
I'm currently adding annotations to a complex module, and even after resolving most of the mypy errors within the module I suspect there are still quite a few inaccurately typed functions due to corner-cases which will only be detected in code that uses the module in specific ways. I already have unittests for this module (written using pytest) that do precisely this, so type checking these would be the ideal way to check my annotations -- all-in-one static and runtime analysis.
It would go a long way to easing adoption if mypy raised errors on type assertions that it can detect are false. e.g.:
Bonus points for supporting the python
unittest
module'sassert*()
methods, but most people these days are usingpytest
and as a feature request it seems much easier and more useful to simply supportassert
.The text was updated successfully, but these errors were encountered: