Skip to content

Commit ffab4d4

Browse files
committed
don't report warn-redundant-cast to cast of Union of Any
1 parent d87bac9 commit ffab4d4

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4687,7 +4687,7 @@ def visit_cast_expr(self, expr: CastExpr) -> Type:
46874687
options = self.chk.options
46884688
if (
46894689
options.warn_redundant_casts
4690-
and not isinstance(get_proper_type(target_type), AnyType)
4690+
and not is_same_type(target_type, AnyType(TypeOfAny.special_form))
46914691
and is_same_type(source_type, target_type)
46924692
):
46934693
self.msg.redundant_cast(target_type, expr)

test-data/unit/check-warnings.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ cast(int, 1)
5757
[out]
5858
main:4: error: Redundant cast to "int"
5959

60+
[case testCastFromUnionOfAnyOk]
61+
# flags: --warn-redundant-casts
62+
from typing import Any, cast, Union
63+
64+
x = Any
65+
y = Any
66+
z = Any
67+
68+
def f(q: Union[x, y, z]) -> None:
69+
cast(Union[x, y], q)
70+
6071
-- Unused 'type: ignore' comments
6172
-- ------------------------------
6273

0 commit comments

Comments
 (0)