6464 FunctionContext , FunctionSigContext ,
6565)
6666from mypy .typeops import (
67- tuple_fallback , make_simplified_union , true_only , false_only , erase_to_union_or_bound ,
68- function_type , callable_type , try_getting_str_literals , custom_special_method ,
67+ try_expanding_sum_type_to_union , tuple_fallback , make_simplified_union ,
68+ true_only , false_only , erase_to_union_or_bound , function_type ,
69+ callable_type , try_getting_str_literals , custom_special_method ,
6970 is_literal_type_like ,
7071)
7172import mypy .errorcodes as codes
@@ -2783,6 +2784,9 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
27832784 # '[1] or []' are inferred correctly.
27842785 ctx = self .type_context [- 1 ]
27852786 left_type = self .accept (e .left , ctx )
2787+ expanded_left_type = try_expanding_sum_type_to_union (
2788+ self .accept (e .left , ctx ), "builtins.bool"
2789+ )
27862790
27872791 assert e .op in ('and' , 'or' ) # Checked by visit_op_expr
27882792
@@ -2817,7 +2821,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
28172821 # to be unreachable and therefore any errors found in the right branch
28182822 # should be suppressed.
28192823 with (self .msg .disable_errors () if right_map is None else nullcontext ()):
2820- right_type = self .analyze_cond_branch (right_map , e .right , left_type )
2824+ right_type = self .analyze_cond_branch (right_map , e .right , expanded_left_type )
28212825
28222826 if right_map is None :
28232827 # The boolean expression is statically known to be the left value
@@ -2829,11 +2833,11 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
28292833 return right_type
28302834
28312835 if e .op == 'and' :
2832- restricted_left_type = false_only (left_type )
2833- result_is_left = not left_type .can_be_true
2836+ restricted_left_type = false_only (expanded_left_type )
2837+ result_is_left = not expanded_left_type .can_be_true
28342838 elif e .op == 'or' :
2835- restricted_left_type = true_only (left_type )
2836- result_is_left = not left_type .can_be_false
2839+ restricted_left_type = true_only (expanded_left_type )
2840+ result_is_left = not expanded_left_type .can_be_false
28372841
28382842 if isinstance (restricted_left_type , UninhabitedType ):
28392843 # The left operand can never be the result
0 commit comments