Skip to content
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

Closed
jhance opened this issue Oct 20, 2015 · 4 comments
Closed

Generate warnings for redundant casts #958

jhance opened this issue Oct 20, 2015 · 4 comments
Assignees

Comments

@jhance
Copy link
Collaborator

jhance commented Oct 20, 2015

If we visit cast(x, T) and the inferred type of x is a subtype of T 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).

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 21, 2015

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 # type: ignore is reasonable for cases like this, or maybe it should be possible to either enable/disable all warnings or individual categories of warnings (similar to flake8 lets silence some warnings, for example).

@jhance
Copy link
Collaborator Author

jhance commented Oct 21, 2015

In that case we should implement some feature that allows us to create types of warnings and emit them.

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 22, 2015

I added #962 for adding support for warnings.

@rwbarton
Copy link
Contributor

rwbarton commented May 5, 2016

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.

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 AnyStr to bytes but not str, for example.

@rwbarton rwbarton self-assigned this Jun 11, 2016
rwbarton added a commit to rwbarton/mypy that referenced this issue Jun 13, 2016
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.
ddfisher pushed a commit that referenced this issue Jun 15, 2016
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants