@@ -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 )
0 commit comments