Skip to content

Commit 1677efd

Browse files
authored
Always allow to cast to Any without warnings. (#8544)
1 parent 5d94c2b commit 1677efd

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

mypy/checkexpr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3004,7 +3004,8 @@ def visit_cast_expr(self, expr: CastExpr) -> Type:
30043004
allow_none_return=True, always_allow_any=True)
30053005
target_type = expr.type
30063006
options = self.chk.options
3007-
if options.warn_redundant_casts and is_same_type(source_type, target_type):
3007+
if (options.warn_redundant_casts and not isinstance(get_proper_type(target_type), AnyType)
3008+
and is_same_type(source_type, target_type)):
30083009
self.msg.redundant_cast(target_type, expr)
30093010
if options.disallow_any_unimported and has_any_from_unimported_type(target_type):
30103011
self.msg.unimported_type_becomes_any("Target type of cast", target_type, expr)

test-data/unit/check-warnings.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ b = B()
3535
c = add([cast(A, b)], [a])
3636
[builtins fixtures/list.pyi]
3737

38+
[case testCastToAnyTypeNotRedundant]
39+
# flags: --warn-redundant-casts
40+
from typing import cast, Any
41+
a: Any
42+
b = cast(Any, a)
43+
[builtins fixtures/list.pyi]
3844

3945
-- Unused 'type: ignore' comments
4046
-- ------------------------------

0 commit comments

Comments
 (0)