Skip to content

Commit 79f4a4b

Browse files
gvanrossumddfisher
authored andcommitted
Kill the remaining redundant casts. (#1827)
Ran `mypy --warn-redundant-casts mypy`.
1 parent 4c11bb3 commit 79f4a4b

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

mypy/checker.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> Type:
917917
def check_assignment(self, lvalue: Node, rvalue: Node, infer_lvalue_type: bool = True) -> None:
918918
"""Type check a single assignment: lvalue = rvalue."""
919919
if isinstance(lvalue, TupleExpr) or isinstance(lvalue, ListExpr):
920-
ltuple = cast(Union[TupleExpr, ListExpr], lvalue)
921-
922-
self.check_assignment_to_multiple_lvalues(ltuple.items, rvalue, lvalue,
920+
self.check_assignment_to_multiple_lvalues(lvalue.items, rvalue, lvalue,
923921
infer_lvalue_type)
924922
else:
925923
lvalue_type, index_lvalue, inferred = self.check_lvalue(lvalue)
@@ -978,7 +976,7 @@ def check_assignment_to_multiple_lvalues(self, lvalues: List[Node], rvalue: Node
978976
# control in cases like: a, b = [int, str] where rhs would get
979977
# type List[object]
980978

981-
rvalues = cast(Union[TupleExpr, ListExpr], rvalue).items
979+
rvalues = rvalue.items
982980

983981
if self.check_rvalue_count_in_assignment(lvalues, len(rvalues), context):
984982
star_index = next((i for i, lv in enumerate(lvalues) if
@@ -1164,8 +1162,7 @@ def check_lvalue(self, lvalue: Node) -> Tuple[Type, IndexExpr, Var]:
11641162
lvalue_type = self.expr_checker.analyze_ref_expr(lvalue, lvalue=True)
11651163
self.store_type(lvalue, lvalue_type)
11661164
elif isinstance(lvalue, TupleExpr) or isinstance(lvalue, ListExpr):
1167-
lv = cast(Union[TupleExpr, ListExpr], lvalue)
1168-
types = [self.check_lvalue(sub_expr)[0] for sub_expr in lv.items]
1165+
types = [self.check_lvalue(sub_expr)[0] for sub_expr in lvalue.items]
11691166
lvalue_type = TupleType(types, self.named_type('builtins.tuple'))
11701167
else:
11711168
lvalue_type = self.accept(lvalue)
@@ -1663,7 +1660,6 @@ def visit_del_stmt(self, s: DelStmt) -> Type:
16631660
def flatten(t: Node) -> List[Node]:
16641661
"""Flatten a nested sequence of tuples/lists into one list of nodes."""
16651662
if isinstance(t, TupleExpr) or isinstance(t, ListExpr):
1666-
t = cast(Union[TupleExpr, ListExpr], t)
16671663
return [b for a in t.items for b in flatten(a)]
16681664
else:
16691665
return [t]

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def infer_function_type_arguments_using_context(
411411
ret_type = callable.ret_type
412412
if isinstance(ret_type, TypeVarType):
413413
if ret_type.values or (not isinstance(ctx, Instance) or
414-
not cast(Instance, ctx).args):
414+
not ctx.args):
415415
# The return type is a type variable. If it has values, we can't easily restrict
416416
# type inference to conform to the valid values. If it's unrestricted, we could
417417
# infer a too general type for the type variable if we use context, and this could

mypy/semanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
10581058
self.lookup_qualified,
10591059
self.lookup_fully_qualified,
10601060
self.fail)
1061-
if res and (not isinstance(res, Instance) or cast(Instance, res).args):
1061+
if res and (not isinstance(res, Instance) or res.args):
10621062
# TODO: What if this gets reassigned?
10631063
name = s.lvalues[0]
10641064
node = self.lookup(name.name, name)
@@ -1410,7 +1410,7 @@ def process_namedtuple_definition(self, s: AssignmentStmt) -> None:
14101410
"""Check if s defines a namedtuple; if yes, store the definition in symbol table."""
14111411
if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], NameExpr):
14121412
return
1413-
lvalue = cast(NameExpr, s.lvalues[0])
1413+
lvalue = s.lvalues[0]
14141414
name = lvalue.name
14151415
named_tuple = self.check_namedtuple(s.rvalue, name)
14161416
if named_tuple is None:

mypy/subtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def unify_generic_callable(type: CallableType, target: CallableType,
338338
applied = mypy.applytype.apply_generic_arguments(type, inferred_vars, msg, context=target)
339339
if msg.is_errors() or not isinstance(applied, CallableType):
340340
return None
341-
return cast(CallableType, applied)
341+
return applied
342342

343343

344344
def restrict_subtype_away(t: Type, s: Type) -> Type:

0 commit comments

Comments
 (0)