Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66c7148

Browse files
elazarggvanrossum
authored andcommitted
Remove/assert casts (#2272)
1 parent 075a8da commit 66c7148

8 files changed

Lines changed: 28 additions & 22 deletions

File tree

mypy/checker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,9 +1323,9 @@ def check_lvalue(self, lvalue: Lvalue) -> Tuple[Type, IndexExpr, Var]:
13231323
inferred = cast(Var, lvalue.node)
13241324
assert isinstance(inferred, Var)
13251325
else:
1326-
m = cast(MemberExpr, lvalue)
1327-
self.accept(m.expr)
1328-
inferred = m.def_var
1326+
assert isinstance(lvalue, MemberExpr)
1327+
self.accept(lvalue.expr)
1328+
inferred = lvalue.def_var
13291329
elif isinstance(lvalue, IndexExpr):
13301330
index_lvalue = lvalue
13311331
elif isinstance(lvalue, MemberExpr):

mypy/checkmember.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ def analyze_var(name: str, var: Var, itype: Instance, info: TypeInfo, node: Cont
273273
if var.is_property:
274274
# A property cannot have an overloaded type => the cast
275275
# is fine.
276-
return cast(CallableType, signature).ret_type
276+
assert isinstance(signature, CallableType)
277+
return signature.ret_type
277278
else:
278279
return signature
279280
return t
@@ -485,8 +486,9 @@ def type_object_type_from_function(init_or_new: FuncBase, info: TypeInfo,
485486
return class_callable(signature, info, fallback, special_sig)
486487
else:
487488
# Overloaded __init__/__new__.
489+
assert isinstance(signature, Overloaded)
488490
items = [] # type: List[CallableType]
489-
for item in cast(Overloaded, signature).items():
491+
for item in signature.items():
490492
items.append(class_callable(item, info, fallback, special_sig))
491493
return Overloaded(items)
492494

mypy/checkstrformat.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,11 @@ def check_simple_str_interpolation(self, specifiers: List[ConversionSpecifier],
151151

152152
def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
153153
replacements: Expression) -> None:
154-
dict_with_only_str_literal_keys = (isinstance(replacements, DictExpr) and
155-
all(isinstance(k, (StrExpr, BytesExpr))
156-
for k, v in replacements.items))
157-
if dict_with_only_str_literal_keys:
154+
if (isinstance(replacements, DictExpr) and
155+
all(isinstance(k, (StrExpr, BytesExpr))
156+
for k, v in replacements.items)):
158157
mapping = {} # type: Dict[str, Type]
159-
for k, v in cast(DictExpr, replacements).items:
158+
for k, v in replacements.items:
160159
key_str = cast(StrExpr, k).value
161160
mapping[key_str] = self.accept(v)
162161

mypy/expandtype.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, cast
1+
from typing import Dict, List
22

33
from mypy.types import (
44
Type, Instance, CallableType, TypeVisitor, UnboundType, ErrorType, AnyType,
@@ -85,7 +85,9 @@ def visit_callable_type(self, t: CallableType) -> Type:
8585
def visit_overloaded(self, t: Overloaded) -> Type:
8686
items = [] # type: List[CallableType]
8787
for item in t.items():
88-
items.append(cast(CallableType, item.accept(self)))
88+
new_item = item.accept(self)
89+
assert isinstance(new_item, CallableType)
90+
items.append(new_item)
8991
return Overloaded(items)
9092

9193
def visit_tuple_type(self, t: TupleType) -> Type:

mypy/join.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ def visit_tuple_type(self, t: TupleType) -> Type:
235235
for i in range(t.length()):
236236
items.append(self.join(t.items[i], self.s.items[i]))
237237
# join fallback types if they are different
238-
from typing import cast
239-
return TupleType(items, cast(Instance, join_instances(self.s.fallback, t.fallback)))
238+
fallback = join_instances(self.s.fallback, t.fallback)
239+
assert isinstance(fallback, Instance)
240+
return TupleType(items, fallback)
240241
else:
241242
return self.default(self.s)
242243

mypy/maptype.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, cast
1+
from typing import Dict, List
22

33
from mypy.expandtype import expand_type
44
from mypy.nodes import TypeInfo
@@ -76,7 +76,9 @@ def map_instance_to_direct_supertypes(instance: Instance,
7676
for b in typ.bases:
7777
if b.type == supertype:
7878
env = instance_to_type_environment(instance)
79-
result.append(cast(Instance, expand_type(b, env)))
79+
t = expand_type(b, env)
80+
assert isinstance(t, Instance)
81+
result.append(t)
8082

8183
if result:
8284
return result

mypy/parsetype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Type parser"""
22

3-
from typing import List, Tuple, Union, cast, Optional
3+
from typing import List, Tuple, Union, Optional
44

55
from mypy.types import (
66
Type, UnboundType, TupleType, TypeList, CallableType, StarType,

mypy/typeanal.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
112112
return self.builtin_type('builtins.tuple')
113113
if len(t.args) == 2 and isinstance(t.args[1], EllipsisType):
114114
# Tuple[T, ...] (uniform, variable-length tuple)
115-
node = self.lookup_fqn_func('builtins.tuple')
116-
tuple_info = cast(TypeInfo, node.node)
117-
return Instance(tuple_info, [t.args[0].accept(self)], t.line)
115+
instance = self.builtin_type('builtins.tuple', [t.args[0].accept(self)])
116+
instance.line = t.line
117+
return instance
118118
return self.tuple_type(self.anal_array(t.args))
119119
elif fullname == 'typing.Union':
120120
items = self.anal_array(t.args)
@@ -291,8 +291,8 @@ def anal_var_defs(self, var_defs: List[TypeVarDef]) -> List[TypeVarDef]:
291291

292292
def builtin_type(self, fully_qualified_name: str, args: List[Type] = None) -> Instance:
293293
node = self.lookup_fqn_func(fully_qualified_name)
294-
info = cast(TypeInfo, node.node)
295-
return Instance(info, args or [])
294+
assert isinstance(node.node, TypeInfo)
295+
return Instance(node.node, args or [])
296296

297297
def tuple_type(self, items: List[Type]) -> TupleType:
298298
return TupleType(items, fallback=self.builtin_type('builtins.tuple', [AnyType()]))

0 commit comments

Comments
 (0)