Skip to content

Warn about redundant casts #1705

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

Merged
merged 1 commit into from
Jun 15, 2016
Merged

Conversation

rwbarton
Copy link
Contributor

@rwbarton rwbarton commented 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 #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 python#958.
@rwbarton rwbarton force-pushed the rwbarton-redundant-casts branch from c665ad2 to bea5792 Compare June 13, 2016 12:59
@ddfisher ddfisher merged commit 9b71668 into python:master Jun 15, 2016
@ddfisher
Copy link
Collaborator

Looks good!

carljm pushed a commit to astral-sh/ruff that referenced this pull request Apr 1, 2025
## Summary

Following up from earlier discussion on Discord, this PR adds logic to
flag casts as redundant when the inferred type of the expression is the
same as the target type. It should follow the semantics from
[mypy](python/mypy#1705).

Example:

```python
def f() -> int:
    return 10

# error: [redundant-cast] "Value is already of type `int`"
cast(int, f())
```
hauntsaninja pushed a commit that referenced this pull request Apr 22, 2025
while working on #18540 (which my original prototype based the code on
`warn-redundant-casts`) I noticed the suggestion
[here](#18540 (comment))
would probably make sense to apply to redundant-cast as well!

I also made sure to include the example from the [original
implementation](#1705 (comment))
just to make sure I wasn't regressing that as well since it seemed
related.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants