-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Generate warnings for redundant casts #958
Comments
This sounds like a great idea -- other than the fact that mypy currently doesn't distinguish between warnings and errors :-/ I wonder how other languages with casts do this? One potential issue is that if people use different tools to analyze the same code, these tools might have slightly different type inference capabilities and people may need casts to silence errors generated by some tools. I guess using |
In that case we should implement some feature that allows us to create types of warnings and emit them. |
I added #962 for adding support for warnings. |
Or (moving my comment from #1470) we need to type check the same program in multiple configurations, such as python versions or versions of dependencies. A special case which mypy can handle itself with some extra care is type checking generic functions with type variables with value restrictions, which are type checked once for each combination of legal values; a cast might be needed when specializing |
A cast is considered redundant if the target type of the cast is the same as the inferred type of the expression. A cast to a supertype like `cast(object, 1)` is not considered redundant because such a cast could be needed to work around deficiencies in type inference. Fixes python#958.
A cast is considered redundant if the target type of the cast is the same as the inferred type of the expression. A cast to a supertype like `cast(object, 1)` is not considered redundant because such a cast could be needed to work around deficiencies in type inference. Fixes #958.
If we visit
cast(x, T)
and the inferred type ofx
is a subtype ofT
then we should emit a warning. Unnecessary casts are bad casts.There should also be a way to make this an error instead of a warning (for example, so we can enforce that we don't have any unnecessary casts in the mypy codebase).
The text was updated successfully, but these errors were encountered: