|
46 | 46 | TypedDictType, |
47 | 47 | TypeOfAny, |
48 | 48 | TypeVarTupleType, |
| 49 | + TypeVarType, |
49 | 50 | UninhabitedType, |
50 | 51 | UnionType, |
51 | 52 | UnpackType, |
@@ -342,13 +343,11 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType: |
342 | 343 | new_inner_type = UninhabitedType() |
343 | 344 | for typ in new_inner_types: |
344 | 345 | new_inner_type = join_types(new_inner_type, typ) |
345 | | - new_type = self.construct_sequence_child(current_type, new_inner_type) |
346 | | - if is_subtype(new_type, current_type): |
347 | | - new_type, _ = self.chk.conditional_types_with_intersection( |
348 | | - current_type, [get_type_range(new_type)], o, default=current_type |
349 | | - ) |
| 346 | + if isinstance(current_type, TypeVarType): |
| 347 | + new_bound = self.narrow_sequence_child(current_type.upper_bound, new_inner_type, o) |
| 348 | + new_type = current_type.copy_modified(upper_bound=new_bound) |
350 | 349 | else: |
351 | | - new_type = current_type |
| 350 | + new_type = self.narrow_sequence_child(current_type, new_inner_type, o) |
352 | 351 | return PatternType(new_type, rest_type, captures) |
353 | 352 |
|
354 | 353 | def get_sequence_type(self, t: Type, context: Context) -> Type | None: |
@@ -447,6 +446,16 @@ def expand_starred_pattern_types( |
447 | 446 |
|
448 | 447 | return new_types |
449 | 448 |
|
| 449 | + def narrow_sequence_child(self, outer_type: Type, inner_type: Type, ctx: Context) -> Type: |
| 450 | + new_type = self.construct_sequence_child(outer_type, inner_type) |
| 451 | + if is_subtype(new_type, outer_type): |
| 452 | + new_type, _ = self.chk.conditional_types_with_intersection( |
| 453 | + outer_type, [get_type_range(new_type)], ctx, default=outer_type |
| 454 | + ) |
| 455 | + else: |
| 456 | + new_type = outer_type |
| 457 | + return new_type |
| 458 | + |
450 | 459 | def visit_starred_pattern(self, o: StarredPattern) -> PatternType: |
451 | 460 | captures: dict[Expression, Type] = {} |
452 | 461 | if o.capture is not None: |
|
0 commit comments