64
64
FunctionContext , FunctionSigContext ,
65
65
)
66
66
from 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 ,
69
70
is_literal_type_like ,
70
71
)
71
72
import mypy .errorcodes as codes
@@ -2783,6 +2784,9 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
2783
2784
# '[1] or []' are inferred correctly.
2784
2785
ctx = self .type_context [- 1 ]
2785
2786
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
+ )
2786
2790
2787
2791
assert e .op in ('and' , 'or' ) # Checked by visit_op_expr
2788
2792
@@ -2817,7 +2821,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
2817
2821
# to be unreachable and therefore any errors found in the right branch
2818
2822
# should be suppressed.
2819
2823
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 )
2821
2825
2822
2826
if right_map is None :
2823
2827
# 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:
2829
2833
return right_type
2830
2834
2831
2835
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
2834
2838
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
2837
2841
2838
2842
if isinstance (restricted_left_type , UninhabitedType ):
2839
2843
# The left operand can never be the result
0 commit comments