python / python/mypy

--warn-redundant-cast doesn’t warn for simple generic types

Open
#21,796 1 comment 0 reactions 0 assignees View on GitHub

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

https://mypy-play.net/?mypy=latest&python=3.12&flags=warn-redundant-casts&gist=f923e1fafbc60a2b7a2fa3c9c3ceb785

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.