Skip to content

Commit a3f002f

Browse files
authored
Remove more redundant casts (#1699)
1 parent 07078e0 commit a3f002f

File tree

10 files changed

+31
-35
lines changed

10 files changed

+31
-35
lines changed

mypy/checkmember.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def analyze_member_access(name: str, typ: Type, node: Context, is_lvalue: bool,
5151
if method:
5252
if method.is_property:
5353
assert isinstance(method, OverloadedFuncDef)
54-
method = cast(OverloadedFuncDef, method)
5554
return analyze_var(name, method.items[0].var, typ, info, node, is_lvalue, msg,
5655
not_ready_callback)
5756
if is_lvalue:
@@ -204,7 +203,7 @@ def analyze_var(name: str, var: Var, itype: Instance, info: TypeInfo, node: Cont
204203
# Class-level function objects and classmethods become bound
205204
# methods: the former to the instance, the latter to the
206205
# class.
207-
functype = cast(FunctionLike, t)
206+
functype = t
208207
check_method_type(functype, itype, var.is_classmethod, node, msg)
209208
signature = method_type(functype)
210209
if var.is_property:

mypy/checkstrformat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
137137
replacements: Node) -> None:
138138
dict_with_only_str_literal_keys = (isinstance(replacements, DictExpr) and
139139
all(isinstance(k, (StrExpr, BytesExpr))
140-
for k, v in cast(DictExpr, replacements).items))
140+
for k, v in replacements.items))
141141
if dict_with_only_str_literal_keys:
142142
mapping = {} # type: Dict[str, Type]
143143
for k, v in cast(DictExpr, replacements).items:

mypy/expandtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def visit_instance(self, t: Instance) -> Type:
7070
def visit_type_var(self, t: TypeVarType) -> Type:
7171
repl = self.variables.get(t.id, t)
7272
if isinstance(repl, Instance):
73-
inst = cast(Instance, repl)
73+
inst = repl
7474
# Return copy of instance with type erasure flag on.
7575
return Instance(inst.type, inst.args, inst.line, True)
7676
else:

mypy/fixup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def lookup_qualified_stnode(modules: Dict[str, MypyFile], name: str) -> SymbolTa
250250
return stnode
251251
node = stnode.node
252252
assert isinstance(node, TypeInfo)
253-
names = cast(TypeInfo, node).names
253+
names = node.names
254254

255255

256256
def store_qualified(modules: Dict[str, MypyFile], name: str, info: SymbolNode) -> None:
@@ -278,4 +278,4 @@ def store_qualified(modules: Dict[str, MypyFile], name: str, info: SymbolNode) -
278278
stnode.node = info
279279
return
280280
assert isinstance(node, TypeInfo)
281-
names = cast(TypeInfo, node).names
281+
names = node.names

mypy/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ def deserialize(cls, data: JsonDict) -> 'SymbolTable':
21382138
def function_type(func: FuncBase, fallback: 'mypy.types.Instance') -> 'mypy.types.FunctionLike':
21392139
if func.type:
21402140
assert isinstance(func.type, mypy.types.FunctionLike)
2141-
return cast(mypy.types.FunctionLike, func.type)
2141+
return func.type
21422142
else:
21432143
# Implicit type signature with dynamic types.
21442144
# Overloaded functions always have a signature, so func must be an ordinary function.

mypy/parse.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ def parse_block(self, allow_type: bool = False) -> Tuple[Block, Type]:
867867
if allow_type:
868868
cur = self.current()
869869
if type is None and isinstance(cur, StrLit):
870-
ds = docstring.parse_docstring(cast(StrLit, cur).parsed())
870+
ds = docstring.parse_docstring(cur.parsed())
871871
if ds and False: # TODO: Enable when this is working.
872872
try:
873873
type = parse_str_as_signature(ds.as_type_str(), cur.line)
@@ -894,15 +894,13 @@ def parse_block(self, allow_type: bool = False) -> Tuple[Block, Type]:
894894

895895
def try_combine_overloads(self, s: Node, stmt: List[Node]) -> bool:
896896
if isinstance(s, Decorator) and stmt:
897-
fdef = cast(Decorator, s)
897+
fdef = s
898898
n = fdef.func.name()
899-
if (isinstance(stmt[-1], Decorator) and
900-
(cast(Decorator, stmt[-1])).func.name() == n):
901-
stmt[-1] = OverloadedFuncDef([cast(Decorator, stmt[-1]), fdef])
899+
if isinstance(stmt[-1], Decorator) and stmt[-1].func.name() == n:
900+
stmt[-1] = OverloadedFuncDef([stmt[-1], fdef])
902901
return True
903-
elif (isinstance(stmt[-1], OverloadedFuncDef) and
904-
(cast(OverloadedFuncDef, stmt[-1])).name() == n):
905-
(cast(OverloadedFuncDef, stmt[-1])).items.append(fdef)
902+
elif isinstance(stmt[-1], OverloadedFuncDef) and stmt[-1].name() == n:
903+
stmt[-1].items.append(fdef)
906904
return True
907905
return False
908906

@@ -1450,7 +1448,7 @@ def parse_list_expr(self) -> Node:
14501448
items[0] = self.parse_generator_expr(items[0])
14511449
self.expect(']')
14521450
if len(items) == 1 and isinstance(items[0], GeneratorExpr):
1453-
return ListComprehension(cast(GeneratorExpr, items[0]))
1451+
return ListComprehension(items[0])
14541452
else:
14551453
expr = ListExpr(items)
14561454
return expr
@@ -1698,7 +1696,7 @@ def parse_member_expr(self, expr: Any) -> Node:
16981696
self.expect('.')
16991697
name = self.expect_type(Name)
17001698
if (isinstance(expr, CallExpr) and isinstance(expr.callee, NameExpr)
1701-
and cast(NameExpr, expr.callee).name == 'super'):
1699+
and expr.callee.name == 'super'):
17021700
# super() expression
17031701
node = SuperExpr(name.string) # type: Node
17041702
else:

mypy/semanal.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def visit_func_def(self, defn: FuncDef) -> None:
302302
original_def = symbol.node
303303
if self.is_conditional_func(original_def, defn):
304304
# Conditional function definition -- multiple defs are ok.
305-
defn.original_def = cast(FuncDef, original_def)
305+
defn.original_def = original_def
306306
else:
307307
# Report error.
308308
self.check_no_global(defn.name(), defn, True)
@@ -695,7 +695,7 @@ def clean_up_bases_and_infer_type_variables(self, defn: ClassDef) -> None:
695695
def analyze_typevar_declaration(self, t: Type) -> Optional[List[Tuple[str, TypeVarExpr]]]:
696696
if not isinstance(t, UnboundType):
697697
return None
698-
unbound = cast(UnboundType, t)
698+
unbound = t
699699
sym = self.lookup_qualified(unbound.name, unbound)
700700
if sym is None or sym.node is None:
701701
return None
@@ -714,7 +714,7 @@ def analyze_typevar_declaration(self, t: Type) -> Optional[List[Tuple[str, TypeV
714714
def analyze_unbound_tvar(self, t: Type) -> Tuple[str, TypeVarExpr]:
715715
if not isinstance(t, UnboundType):
716716
return None
717-
unbound = cast(UnboundType, t)
717+
unbound = t
718718
sym = self.lookup_qualified(unbound.name, unbound)
719719
if sym is not None and sym.kind == UNBOUND_TVAR:
720720
return unbound.name, cast(TypeVarExpr, sym.node)
@@ -1055,7 +1055,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
10551055
self.fail)
10561056
if res and (not isinstance(res, Instance) or cast(Instance, res).args):
10571057
# TODO: What if this gets reassigned?
1058-
name = cast(NameExpr, s.lvalues[0])
1058+
name = s.lvalues[0]
10591059
node = self.lookup(name.name, name)
10601060
node.kind = TYPE_ALIAS
10611061
node.type_override = res
@@ -1213,8 +1213,8 @@ def is_self_member_ref(self, memberexpr: MemberExpr) -> bool:
12131213
"""Does memberexpr to refer to an attribute of self?"""
12141214
if not isinstance(memberexpr.expr, NameExpr):
12151215
return False
1216-
node = (cast(NameExpr, memberexpr.expr)).node
1217-
return isinstance(node, Var) and (cast(Var, node)).is_self
1216+
node = memberexpr.expr.node
1217+
return isinstance(node, Var) and node.is_self
12181218

12191219
def check_lvalue_validity(self, node: Node, ctx: Context) -> None:
12201220
if isinstance(node, (TypeInfo, TypeVarExpr)):
@@ -1308,10 +1308,10 @@ def get_typevar_declaration(self, s: AssignmentStmt) -> Optional[CallExpr]:
13081308
return None
13091309
if not isinstance(s.rvalue, CallExpr):
13101310
return None
1311-
call = cast(CallExpr, s.rvalue)
1311+
call = s.rvalue
13121312
if not isinstance(call.callee, RefExpr):
13131313
return None
1314-
callee = cast(RefExpr, call.callee)
1314+
callee = call.callee
13151315
if callee.fullname != 'typing.TypeVar':
13161316
return None
13171317
return call
@@ -1406,10 +1406,10 @@ def check_namedtuple(self, node: Node, var_name: str = None) -> TypeInfo:
14061406
"""
14071407
if not isinstance(node, CallExpr):
14081408
return None
1409-
call = cast(CallExpr, node)
1409+
call = node
14101410
if not isinstance(call.callee, RefExpr):
14111411
return None
1412-
callee = cast(RefExpr, call.callee)
1412+
callee = call.callee
14131413
fullname = callee.fullname
14141414
if fullname not in ('collections.namedtuple', 'typing.NamedTuple'):
14151415
return None
@@ -2589,7 +2589,7 @@ def builtin_type(self, name: str, args: List[Type] = None) -> Instance:
25892589
names = self.modules['builtins']
25902590
sym = names.names[name]
25912591
assert isinstance(sym.node, TypeInfo)
2592-
return Instance(cast(TypeInfo, sym.node), args or [])
2592+
return Instance(sym.node, args or [])
25932593

25942594

25952595
def self_type(typ: TypeInfo) -> Union[Instance, TupleType]:

mypy/stats.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ def visit_type_application(self, o: TypeApplication) -> None:
8888
def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
8989
self.line = o.line
9090
if (isinstance(o.rvalue, nodes.CallExpr) and
91-
isinstance(cast(nodes.CallExpr, o.rvalue).analyzed,
92-
nodes.TypeVarExpr)):
91+
isinstance(o.rvalue.analyzed, nodes.TypeVarExpr)):
9392
# Type variable definition -- not a real assignment.
9493
return
9594
if o.type:
@@ -256,7 +255,7 @@ def visit_callable_type(self, t: CallableType) -> bool:
256255

257256

258257
def is_generic(t: Type) -> bool:
259-
return isinstance(t, Instance) and bool(cast(Instance, t).args)
258+
return isinstance(t, Instance) and bool(t.args)
260259

261260

262261
def is_complex(t: Type) -> bool:

mypy/subtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def is_subtype(left: Type, right: Type,
4646
return True
4747
elif isinstance(right, UnionType) and not isinstance(left, UnionType):
4848
return any(is_subtype(left, item, type_parameter_checker)
49-
for item in cast(UnionType, right).items)
49+
for item in right.items)
5050
else:
5151
return left.accept(SubtypeVisitor(right, type_parameter_checker))
5252

mypy/typeanal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
110110
if len(t.args) == 2 and isinstance(t.args[1], EllipsisType):
111111
# Tuple[T, ...] (uniform, variable-length tuple)
112112
node = self.lookup_fqn_func('builtins.tuple')
113-
info = cast(TypeInfo, node.node)
114-
return Instance(info, [t.args[0].accept(self)], t.line)
113+
tuple_info = cast(TypeInfo, node.node)
114+
return Instance(tuple_info, [t.args[0].accept(self)], t.line)
115115
return self.tuple_type(self.anal_array(t.args))
116116
elif fullname == 'typing.Union':
117117
items = self.anal_array(t.args)
@@ -152,7 +152,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
152152
return AnyType()
153153
self.fail('Invalid type "{}"'.format(name), t)
154154
return t
155-
info = cast(TypeInfo, sym.node)
155+
info = sym.node # type: TypeInfo
156156
if len(t.args) > 0 and info.fullname() == 'builtins.tuple':
157157
return TupleType(self.anal_array(t.args),
158158
Instance(info, [AnyType()], t.line),

0 commit comments

Comments
 (0)