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