Skip to content

Commit

Permalink
Factor out two checks
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw committed Oct 24, 2024
1 parent e9cc4b2 commit c5a3ce6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 18 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5689,7 +5689,7 @@ def _format_expr_type(self, t: Type, expr: Expression) -> str:
else:
return f"Expression has type {typ}"

def check_for_truthy_type(self, t: Type, expr: Expression) -> None:
def _check_for_truthy_type(self, t: Type, expr: Expression) -> None:
"""
Check if a type can have a truthy value.
Expand Down Expand Up @@ -5731,7 +5731,7 @@ def get_expr_name() -> str:
else:
self.fail(message_registry.TYPE_ALWAYS_TRUE.format(format_expr_type()), expr)

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

def check_for_appropriate_truthiness_in_boolean_context(
self, t: Type, expr: Expression
) -> None:
"""Check if a type is truthy or potentially-falsy when it shouldn't be.
Used in checks like::
if x: # <---
not x # <---
"""
self._check_for_truthy_type(t, expr)
self._check_for_optional_non_truthy_type(t, expr)

def find_type_equals_check(
self, node: ComparisonExpr, expr_indices: list[int]
) -> tuple[TypeMap, TypeMap]:
Expand Down Expand Up @@ -6212,9 +6227,7 @@ def has_no_custom_eq_checks(t: Type) -> bool:
if in_boolean_context:
# We don't check `:=` values in expressions like `(a := A())`,
# because they produce two error messages.
# FIXME: make this a single call again
self.check_for_truthy_type(original_vartype, node)
self.check_for_optional_non_truthy_type(original_vartype, node)
self.check_for_appropriate_truthiness_in_boolean_context(original_vartype, node)
vartype = try_expanding_sum_type_to_union(original_vartype, "builtins.bool")

if_type = true_only(vartype)
Expand Down
4 changes: 1 addition & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4296,9 +4296,7 @@ def visit_unary_expr(self, e: UnaryExpr) -> Type:
op = e.op
if op == "not":
result: Type = self.bool_type()
# FIXME: make this a single call again
self.chk.check_for_truthy_type(operand_type, e.expr)
self.chk.check_for_optional_non_truthy_type(operand_type, e.expr)
self.chk.check_for_appropriate_truthiness_in_boolean_context(operand_type, e.expr)
else:
method = operators.unary_op_methods[op]
result, method_type = self.check_method_call_by_name(method, operand_type, [], [], e)
Expand Down

0 comments on commit c5a3ce6

Please sign in to comment.