diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85..a6cf1cab6470 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -5036,10 +5036,7 @@ def visit_enum_index_expr( def visit_cast_expr(self, expr: CastExpr) -> Type: """Type check a cast expression.""" source_type = self.accept( - expr.expr, - type_context=AnyType(TypeOfAny.special_form), - allow_none_return=True, - always_allow_any=True, + expr.expr, type_context=None, allow_none_return=True, always_allow_any=True ) target_type = expr.type options = self.chk.options diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index 8ab1f398ba42..1a8abe689429 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -282,3 +282,16 @@ def identity(xs: list[T]) -> list[T]: def f(xs: list[int]) -> list[int]: return cast(list[int], identity(xs)) # E: Redundant cast to "list[int]" + +[case testRedundantCastGenericCallWithTuple] +# flags: --warn-redundant-casts +from typing import Callable, TypeVar, cast + +T = TypeVar("T") + +class A: ... + +def f(factory: Callable[[], T]) -> tuple[A, T]: ... + +cast(tuple[A, A], f(A)) # E: Redundant cast to "tuple[A, A]" +[builtins fixtures/tuple.pyi]