|
14 | 14 | from mypy.maptype import map_instance_to_supertype |
15 | 15 | from mypy.meet import narrow_declared_type |
16 | 16 | from mypy.messages import MessageBuilder |
17 | | -from mypy.nodes import ARG_POS, Context, Expression, NameExpr, TempNode, TypeAlias, Var |
| 17 | +from mypy.nodes import ARG_POS, Expression, NameExpr, TempNode, TypeAlias, Var |
18 | 18 | from mypy.options import Options |
19 | 19 | from mypy.patterns import ( |
20 | 20 | AsPattern, |
@@ -394,11 +394,14 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType: |
394 | 394 | new_inner_type = UninhabitedType() |
395 | 395 | for typ in new_inner_types: |
396 | 396 | new_inner_type = join_types(new_inner_type, typ) |
397 | | - if isinstance(current_type, TypeVarType): |
398 | | - new_bound = self.narrow_sequence_child(current_type.upper_bound, new_inner_type, o) |
399 | | - new_type = current_type.copy_modified(upper_bound=new_bound) |
400 | | - else: |
401 | | - new_type = self.narrow_sequence_child(current_type, new_inner_type, o) |
| 397 | + new_type = self.construct_sequence_child(current_type, new_inner_type) |
| 398 | + new_type, possible_rest_type = self.chk.conditional_types_with_intersection( |
| 399 | + current_type, [get_type_range(new_type)], o, default=current_type |
| 400 | + ) |
| 401 | + if star_position is not None and len(o.patterns) == 1: |
| 402 | + # Match cannot be refuted, so narrow the remaining type |
| 403 | + rest_type = possible_rest_type |
| 404 | + |
402 | 405 | return PatternType(new_type, rest_type, captures) |
403 | 406 |
|
404 | 407 | def contract_starred_pattern_types( |
@@ -478,16 +481,6 @@ def expand_starred_pattern_types( |
478 | 481 |
|
479 | 482 | return new_types |
480 | 483 |
|
481 | | - def narrow_sequence_child(self, outer_type: Type, inner_type: Type, ctx: Context) -> Type: |
482 | | - new_type = self.construct_sequence_child(outer_type, inner_type) |
483 | | - if is_subtype(new_type, outer_type): |
484 | | - new_type, _ = self.chk.conditional_types_with_intersection( |
485 | | - outer_type, [get_type_range(new_type)], ctx, default=outer_type |
486 | | - ) |
487 | | - else: |
488 | | - new_type = outer_type |
489 | | - return new_type |
490 | | - |
491 | 484 | def visit_starred_pattern(self, o: StarredPattern) -> PatternType: |
492 | 485 | captures: dict[Expression, Type] = {} |
493 | 486 | if o.capture is not None: |
@@ -796,6 +789,9 @@ def construct_sequence_child(self, outer_type: Type, inner_type: Type) -> Type: |
796 | 789 | or class T(Sequence[Tuple[T, T]]), there is no way any of those can map to Sequence[str]. |
797 | 790 | """ |
798 | 791 | proper_type = get_proper_type(outer_type) |
| 792 | + if isinstance(proper_type, TypeVarType): |
| 793 | + new_bound = self.construct_sequence_child(proper_type.upper_bound, inner_type) |
| 794 | + return proper_type.copy_modified(upper_bound=new_bound) |
799 | 795 | if isinstance(proper_type, AnyType): |
800 | 796 | return outer_type |
801 | 797 | if isinstance(proper_type, UnionType): |
|
0 commit comments