|
62 | 62 | FunctionContext, FunctionSigContext,
|
63 | 63 | )
|
64 | 64 | from mypy.typeops import (
|
65 |
| - tuple_fallback, make_simplified_union, true_only, false_only, erase_to_union_or_bound, |
66 |
| - function_type, callable_type, try_getting_str_literals, custom_special_method, |
| 65 | + try_expanding_sum_type_to_union, tuple_fallback, make_simplified_union, |
| 66 | + true_only, false_only, erase_to_union_or_bound, function_type, |
| 67 | + callable_type, try_getting_str_literals, custom_special_method, |
67 | 68 | is_literal_type_like,
|
68 | 69 | )
|
69 | 70 | import mypy.errorcodes as codes
|
@@ -2775,17 +2776,20 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
|
2775 | 2776 | # '[1] or []' are inferred correctly.
|
2776 | 2777 | ctx = self.type_context[-1]
|
2777 | 2778 | left_type = self.accept(e.left, ctx)
|
| 2779 | + expanded_left_type = try_expanding_sum_type_to_union( |
| 2780 | + self.accept(e.left, ctx), "builtins.bool" |
| 2781 | + ) |
2778 | 2782 |
|
2779 | 2783 | assert e.op in ('and', 'or') # Checked by visit_op_expr
|
2780 | 2784 |
|
2781 | 2785 | if e.op == 'and':
|
2782 | 2786 | right_map, left_map = self.chk.find_isinstance_check(e.left)
|
2783 |
| - restricted_left_type = false_only(left_type) |
2784 |
| - result_is_left = not left_type.can_be_true |
| 2787 | + restricted_left_type = false_only(expanded_left_type) |
| 2788 | + result_is_left = not expanded_left_type.can_be_true |
2785 | 2789 | elif e.op == 'or':
|
2786 | 2790 | left_map, right_map = self.chk.find_isinstance_check(e.left)
|
2787 |
| - restricted_left_type = true_only(left_type) |
2788 |
| - result_is_left = not left_type.can_be_false |
| 2791 | + restricted_left_type = true_only(expanded_left_type) |
| 2792 | + result_is_left = not expanded_left_type.can_be_false |
2789 | 2793 |
|
2790 | 2794 | # If left_map is None then we know mypy considers the left expression
|
2791 | 2795 | # to be redundant.
|
@@ -2818,7 +2822,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
|
2818 | 2822 | if right_map is None:
|
2819 | 2823 | self.msg.disable_errors()
|
2820 | 2824 | try:
|
2821 |
| - right_type = self.analyze_cond_branch(right_map, e.right, left_type) |
| 2825 | + right_type = self.analyze_cond_branch(right_map, e.right, expanded_left_type) |
2822 | 2826 | finally:
|
2823 | 2827 | if right_map is None:
|
2824 | 2828 | self.msg.enable_errors()
|
|
0 commit comments