Skip to content

Commit c5a3ce6

Browse files
committed
Factor out two checks
1 parent e9cc4b2 commit c5a3ce6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

mypy/checker.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5689,7 +5689,7 @@ def _format_expr_type(self, t: Type, expr: Expression) -> str:
56895689
else:
56905690
return f"Expression has type {typ}"
56915691

5692-
def check_for_truthy_type(self, t: Type, expr: Expression) -> None:
5692+
def _check_for_truthy_type(self, t: Type, expr: Expression) -> None:
56935693
"""
56945694
Check if a type can have a truthy value.
56955695
@@ -5731,7 +5731,7 @@ def get_expr_name() -> str:
57315731
else:
57325732
self.fail(message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()), expr)
57335733

5734-
def check_for_optional_non_truthy_type(self, t: Type, expr: Expression) -> None:
5734+
def _check_for_optional_non_truthy_type(self, t: Type, expr: Expression) -> None:
57355735
"""Check if a type involves both None and types that aren't always true, catching
57365736
suspicious cases of falsy values being lumped together with None.
57375737
@@ -5766,6 +5766,21 @@ def check_for_optional_non_truthy_type(self, t: Type, expr: Expression) -> None:
57665766
expr,
57675767
)
57685768

5769+
def check_for_appropriate_truthiness_in_boolean_context(
5770+
self, t: Type, expr: Expression
5771+
) -> None:
5772+
"""Check if a type is truthy or potentially-falsy when it shouldn't be.
5773+
5774+
Used in checks like::
5775+
5776+
if x: # <---
5777+
5778+
not x # <---
5779+
5780+
"""
5781+
self._check_for_truthy_type(t, expr)
5782+
self._check_for_optional_non_truthy_type(t, expr)
5783+
57695784
def find_type_equals_check(
57705785
self, node: ComparisonExpr, expr_indices: list[int]
57715786
) -> tuple[TypeMap, TypeMap]:
@@ -6212,9 +6227,7 @@ def has_no_custom_eq_checks(t: Type) -> bool:
62126227
if in_boolean_context:
62136228
# We don't check `:=` values in expressions like `(a := A())`,
62146229
# because they produce two error messages.
6215-
# FIXME: make this a single call again
6216-
self.check_for_truthy_type(original_vartype, node)
6217-
self.check_for_optional_non_truthy_type(original_vartype, node)
6230+
self.check_for_appropriate_truthiness_in_boolean_context(original_vartype, node)
62186231
vartype = try_expanding_sum_type_to_union(original_vartype, "builtins.bool")
62196232

62206233
if_type = true_only(vartype)

mypy/checkexpr.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,9 +4296,7 @@ def visit_unary_expr(self, e: UnaryExpr) -> Type:
42964296
op = e.op
42974297
if op == "not":
42984298
result: Type = self.bool_type()
4299-
# FIXME: make this a single call again
4300-
self.chk.check_for_truthy_type(operand_type, e.expr)
4301-
self.chk.check_for_optional_non_truthy_type(operand_type, e.expr)
4299+
self.chk.check_for_appropriate_truthiness_in_boolean_context(operand_type, e.expr)
43024300
else:
43034301
method = operators.unary_op_methods[op]
43044302
result, method_type = self.check_method_call_by_name(method, operand_type, [], [], e)

0 commit comments

Comments
 (0)