--warn-redundant-cast doesn’t warn for simple generic types
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
See reproducer
To Reproduce
from typing import TypeVar, cast
T = TypeVar("T")
def identity(xs: list[T]) -> list[T]:
return xs
def f(xs: list[int]) -> list[int]:
reveal_type(identity(xs))
return cast(list[int], identity(xs)) # --warn-redundant-casts should trigger here
Expected Behavior
warns that the cast is redundant
Actual Behavior
doesn’t warn
Your Environment
- Mypy version used: 2.3.0
- Mypy command-line flags:
--warn-redundant-casts - Mypy configuration options from
mypy.ini(and other config files):warn-redundant-casts = true - Python version used: any
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy-play reproducer and run it with --warn-redundant-casts to confirm the missing warning for cast(list[int], identity(xs)). Trace the redundant-cast check for generic list types, then add a regression test showing that this cast is reported while the existing type reveal remains valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100