|
48 | 48 | from mypy.expandtype import expand_type |
49 | 49 | from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash |
50 | 50 | from mypy.maptype import map_instance_to_supertype |
51 | | -from mypy.meet import is_overlapping_erased_types, is_overlapping_types, meet_types |
| 51 | +from mypy.meet import ( |
| 52 | + is_overlapping_erased_types, |
| 53 | + is_overlapping_types, |
| 54 | + meet_types, |
| 55 | + narrow_declared_type, |
| 56 | +) |
52 | 57 | from mypy.message_registry import ErrorMessage |
53 | 58 | from mypy.messages import ( |
54 | 59 | SUGGESTED_TEST_FIXTURES, |
@@ -6292,26 +6297,28 @@ def update_fixed_type(new_fixed_type: Type, new_is_final: bool) -> bool: |
6292 | 6297 | if_maps = [] |
6293 | 6298 | else_maps = [] |
6294 | 6299 | for expr in exprs_in_type_calls: |
6295 | | - expr_type = get_proper_type(self.lookup_type(expr)) |
| 6300 | + expr_type: Type = get_proper_type(self.lookup_type(expr)) |
6296 | 6301 | for type_range in target_types: |
6297 | | - new_expr_type, _ = self.conditional_types_with_intersection( |
| 6302 | + restriction, _ = self.conditional_types_with_intersection( |
6298 | 6303 | expr_type, type_range, expr |
6299 | 6304 | ) |
6300 | | - if new_expr_type is not None: |
6301 | | - new_expr_type = get_proper_type(new_expr_type) |
6302 | | - if isinstance(expr_type, AnyType): |
6303 | | - expr_type = new_expr_type |
6304 | | - elif not isinstance(new_expr_type, AnyType): |
6305 | | - expr_type = meet_types(expr_type, new_expr_type) |
| 6305 | + if restriction is not None: |
| 6306 | + narrowed_type = get_proper_type(narrow_declared_type(expr_type, restriction)) |
| 6307 | + # Cannot be guaranteed that this is unreachable, so use fallback type. |
| 6308 | + if isinstance(narrowed_type, UninhabitedType): |
| 6309 | + expr_type = restriction |
| 6310 | + else: |
| 6311 | + expr_type = narrowed_type |
6306 | 6312 | _, else_map = conditional_types_to_typemaps( |
6307 | 6313 | expr, |
6308 | 6314 | *self.conditional_types_with_intersection( |
6309 | 6315 | (self.lookup_type(expr)), (type_range), expr |
6310 | 6316 | ), |
6311 | 6317 | ) |
6312 | 6318 | else_maps.append(else_map) |
| 6319 | + |
6313 | 6320 | if fixed_type and expr_type is not None: |
6314 | | - expr_type = meet_types(expr_type, fixed_type) |
| 6321 | + expr_type = narrow_declared_type(expr_type, fixed_type) |
6315 | 6322 |
|
6316 | 6323 | if_map, _ = conditional_types_to_typemaps(expr, expr_type, None) |
6317 | 6324 | if_maps.append(if_map) |
|
0 commit comments