10000 simpler _check_for_truthy_type with less crashiness · python/mypy@c84a736 · GitHub
[go: up one dir, main page]

Skip to content

Commit c84a736

Browse files
committed
simpler _check_for_truthy_type with less crashiness
1 parent 550feaf commit c84a736

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

mypy/checker.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,17 +3980,15 @@ def _is_truthy_instance(t: Type) -> bool:
39803980
not t.type.has_readable_member('__len__')
39813981
)
39823982

3983-
def _check_for_truthy_type(self, t: Type, name: str, context: Context) -> None:
3983+
def _check_for_truthy_type(self, t: Type, node: Node) -> None:
39843984
if self._is_truthy_instance(t):
39853985
self.msg.note(
39863986
"{} has type '{}' which does not implement __bool__ or __len__ "
3987-
"so it will always be truthy in boolean context".format(name, t), context)
3988-
elif isinstance(t, UnionType) and all(
3989-
self._is_truthy_instance(t) for t in t.items
3990-
):
3987+
"so it will always be truthy in boolean context".format(node, t), node)
3988+
elif isinstance(t, UnionType) and all(self._is_truthy_instance(t) for t in t.items):
39913989
self.msg.note(
39923990
"{} has type '{}' where none of the members implement __bool__ or __len__ "
3993-
"so it will always be truthy in boolean context".format(name, t), context)
3991+
"so it will always be truthy in boolean context".format(node, t), node)
39943992

39953993
def find_type_equals_check(self, node: ComparisonExpr, expr_indices: List[int]
39963994
) -> Tuple[TypeMap, TypeMap]:
@@ -4089,6 +4087,7 @@ def find_isinstance_check(self, node: Expression
40894087

40904088
def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeMap]:
40914089
type_map = self.type_map
4090+
40924091
if is_true_literal(node):
40934092
return {}, None
40944093
elif is_false_literal(node):
@@ -4125,7 +4124,7 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
41254124
if literal(expr) == LITERAL_TYPE:
41264125
return {expr: TypeGuardType(node.callee.type_guard)}, {}
41274126

4128-
self._check_for_truthy_type(type_map[node], name=f'return value of {node.callee.fullname}', context=node)
4127+
self._check_for_truthy_type(type_map[node], node)
41294128

41304129
return {}, {}
41314130
elif isinstance(node, ComparisonExpr):
@@ -4279,7 +4278,7 @@ def has_no_custom_eq_checks(t: Type) -> bool:
42794278
# Restrict the type of the variable to True-ish/False-ish in the if and else branches
42804279
# respectively
42814280
vartype = type_map[node]
4282-
self._check_for_truthy_type(vartype, name=node.fullname, context=node)
4281+
self._check_for_truthy_type(vartype, node)
42834282
if_type = true_only(vartype) # type: Type
42844283
else_type = false_only(vartype) # type: Type
42854284
ref = node # type: Expression
@@ -4308,7 +4307,6 @@ def has_no_custom_eq_checks(t: Type) -> bool:
43084307
elif isinstance(node, UnaryExpr) and node.op == 'not':
43094308
left, right = self.find_isinstance_check_helper(node.expr)
43104309
return right, left
4311-
43124310
# Not a supported isinstance check
43134311
return {}, {}
43144312

0 commit comments

Comments
 (0)
0