-
-
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
Typecheking code inside try...except TypeError #2420
Comments
As @ilevkivskyi mentioned on Gitter, this can be dangerous. Mypy can't easily tell from where the This could be more effective if mypy would classify errors using error ids (similar to some linters). For example, we could have one id for all errors that are likely to result in However, mypy wouldn't know exactly which exceptions a particular function or method can raise -- one might raise As this is a relatively complex issue, and there are easy workarounds (such as a cast or |
IMO this should not become a feature. Catching an exception is expensive (and even if you don't catch it you pay for the try/except setup and teardown). More importantly, an exception handler would basically end up disabling all type checking in the try/block. We also have no syntax for annotating which functions raise which exceptions. The proper approach should use isinstance(), not cast(), because the latter just shuts up the checker completely while a series of isinstance() checks (in an if..elif... structure) is completely type-checked. |
Is it worth considering reopening this issue? The |
here's an example:
val
could be either astr
or aList[int]
, but if it's a list it'll be caught byexcept TypeError
for this code, mypy returns:
There are workarounds using isinstance or cast, but it might be useful that mypy be able to typecheck this code.
The text was updated successfully, but these errors were encountered: