Skip to content

Commit 043963a

Browse files
committed
Fix redundant cast detection for generic calls
1 parent 630b101 commit 043963a

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

mypy/checkexpr.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4850,10 +4850,7 @@ def visit_enum_index_expr(
48504850
def visit_cast_expr(self, expr: CastExpr) -> Type:
48514851
"""Type check a cast expression."""
48524852
source_type = self.accept(
4853-
expr.expr,
4854-
type_context=AnyType(TypeOfAny.special_form),
4855-
allow_none_return=True,
4856-
always_allow_any=True,
4853+
expr.expr, type_context=None, allow_none_return=True, always_allow_any=True
48574854
)
48584855
target_type = expr.type
48594856
options = self.chk.options

test-data/unit/check-warnings.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ c = cast(int, a)
1212
[out]
1313
main:5: error: Redundant cast to "int"
1414

15+
[case testRedundantCastGenericReturn]
16+
# flags: --warn-redundant-casts
17+
from typing import TypeVar, cast
18+
19+
T = TypeVar("T")
20+
21+
def identity(xs: list[T]) -> list[T]:
22+
return xs
23+
24+
xs: list[int] = []
25+
cast(list[int], identity(xs))
26+
[out]
27+
main:10: error: Redundant cast to "list[int]"
28+
1529
[case testRedundantCastWithIsinstance]
1630
# flags: --warn-redundant-casts
1731
from typing import cast, Union

0 commit comments

Comments
 (0)