Skip to content

Commit d87bac9

Browse files
committed
use is_same_type when determining if a cast is redundant
1 parent f44a60d commit d87bac9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4688,7 +4688,7 @@ def visit_cast_expr(self, expr: CastExpr) -> Type:
46884688
if (
46894689
options.warn_redundant_casts
46904690
and not isinstance(get_proper_type(target_type), AnyType)
4691-
and source_type == target_type
4691+
and is_same_type(source_type, target_type)
46924692
):
46934693
self.msg.redundant_cast(target_type, expr)
46944694
if options.disallow_any_unimported and has_any_from_unimported_type(target_type):

test-data/unit/check-warnings.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ a: Any
4242
b = cast(Any, a)
4343
[builtins fixtures/list.pyi]
4444

45+
[case testCastToObjectNotRedunant]
46+
# flags: --warn-redundant-casts
47+
from typing import cast
48+
49+
a = 1
50+
b = cast(object, 1)
51+
52+
[case testCastFromLiteralRedundant]
53+
# flags: --warn-redundant-casts
54+
from typing import cast
55+
56+
cast(int, 1)
57+
[out]
58+
main:4: error: Redundant cast to "int"
59+
4560
-- Unused 'type: ignore' comments
4661
-- ------------------------------
4762

0 commit comments

Comments
 (0)