Skip to content

Commit 9cedc4f

Browse files
committed
Add TupleGetterType type
1 parent a0665e1 commit 9cedc4f

23 files changed

+173
-8
lines changed

mypy/checkmember.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
ParamSpecType,
6262
PartialType,
6363
ProperType,
64+
TupleGetterType,
6465
TupleType,
6566
Type,
6667
TypedDictType,
@@ -935,12 +936,20 @@ def analyze_var(
935936
return result
936937

937938

939+
def expand_tuplegetter_type_if_needed(typ: Type) -> Type:
940+
proper = get_proper_type(typ)
941+
if isinstance(proper, TupleGetterType):
942+
return proper.typ
943+
return typ
944+
945+
938946
def expand_without_binding(
939947
typ: Type, var: Var, itype: Instance, original_itype: Instance, mx: MemberContext
940948
) -> Type:
941949
if not mx.preserve_type_var_ids:
942950
typ = freshen_all_functions_type_vars(typ)
943951
typ = expand_self_type_if_needed(typ, mx, var, original_itype)
952+
typ = expand_tuplegetter_type_if_needed(typ)
944953
expanded = expand_type_by_instance(typ, itype)
945954
freeze_all_type_vars(expanded)
946955
return expanded

mypy/constraints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
ParamSpecType,
3737
PartialType,
3838
ProperType,
39+
TupleGetterType,
3940
TupleType,
4041
Type,
4142
TypeAliasType,
@@ -1244,6 +1245,9 @@ def infer_against_overloaded(
12441245
item = find_matching_overload_item(overloaded, template)
12451246
return infer_constraints(template, item, self.direction)
12461247

1248+
def visit_tuplegetter_type(self, template: TupleGetterType) -> list[Constraint]:
1249+
raise NotImplementedError
1250+
12471251
def visit_tuple_type(self, template: TupleType) -> list[Constraint]:
12481252
actual = self.actual
12491253
unpack_index = find_unpack_in_list(template.items)

mypy/copytype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ParamSpecType,
1616
PartialType,
1717
ProperType,
18+
TupleGetterType,
1819
TupleType,
1920
TypeAliasType,
2021
TypedDictType,
@@ -103,6 +104,9 @@ def visit_partial_type(self, t: PartialType) -> ProperType:
103104
def visit_callable_type(self, t: CallableType) -> ProperType:
104105
return self.copy_common(t, t.copy_modified())
105106

107+
def visit_tuplegetter_type(self, t: TupleGetterType) -> ProperType:
108+
return self.copy_common(t, TupleGetterType(t.typ))
109+
106110
def visit_tuple_type(self, t: TupleType) -> ProperType:
107111
return self.copy_common(t, TupleType(t.items, t.partial_fallback, implicit=t.implicit))
108112

mypy/erasetype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ParamSpecType,
1818
PartialType,
1919
ProperType,
20+
TupleGetterType,
2021
TupleType,
2122
Type,
2223
TypeAliasType,
@@ -115,6 +116,9 @@ def visit_callable_type(self, t: CallableType) -> ProperType:
115116
def visit_overloaded(self, t: Overloaded) -> ProperType:
116117
return t.fallback.accept(self)
117118

119+
def visit_tuplegetter_type(self, t: TupleGetterType) -> ProperType:
120+
raise NotImplementedError
121+
118122
def visit_tuple_type(self, t: TupleType) -> ProperType:
119123
return t.partial_fallback.accept(self)
120124

mypy/expandtype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
PartialType,
2424
ProperType,
2525
TrivialSyntheticTypeTranslator,
26+
TupleGetterType,
2627
TupleType,
2728
Type,
2829
TypeAliasType,
@@ -456,6 +457,9 @@ def expand_type_tuple_with_unpack(self, typs: tuple[Type, ...]) -> list[Type]:
456457
items.append(item.accept(self))
457458
return items
458459

460+
def visit_tuplegetter_type(self, t: TupleGetterType) -> Type:
461+
return TupleGetterType(t.typ.accept(self))
462+
459463
def visit_tuple_type(self, t: TupleType) -> Type:
460464
items = self.expand_type_list_with_unpack(t.items)
461465
if len(items) == 1:

mypy/fixup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Overloaded,
3030
Parameters,
3131
ParamSpecType,
32+
TupleGetterType,
3233
TupleType,
3334
TypeAliasType,
3435
TypedDictType,
@@ -296,6 +297,9 @@ def visit_uninhabited_type(self, o: Any) -> None:
296297
def visit_partial_type(self, o: Any) -> None:
297298
raise RuntimeError("Shouldn't get here", o)
298299

300+
def visit_tuplegetter_type(self, tgt: TupleGetterType) -> None:
301+
tgt.typ.accept(self)
302+
299303
def visit_tuple_type(self, tt: TupleType) -> None:
300304
if tt.items:
301305
for it in tt.items:

mypy/indirection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def visit_overloaded(self, t: types.Overloaded) -> None:
128128
self._visit_type_list(list(t.items))
129129
self._visit(t.fallback)
130130

131+
def visit_tuplegetter_type(self, t: types.TupleGetterType) -> None:
132+
self._visit(t.typ)
133+
131134
def visit_tuple_type(self, t: types.TupleType) -> None:
132135
self._visit_type_list(t.items)
133136
self._visit(t.partial_fallback)

mypy/join.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
ParamSpecType,
3333
PartialType,
3434
ProperType,
35+
TupleGetterType,
3536
TupleType,
3637
Type,
3738
TypeAliasType,
@@ -559,6 +560,9 @@ def join_tuples(self, s: TupleType, t: TupleType) -> list[Type] | None:
559560
items.append(join_types(fi, vi))
560561
return items
561562

563+
def visit_tuplegetter_type(self, t: TupleGetterType) -> ProperType:
564+
raise NotImplementedError
565+
562566
def visit_tuple_type(self, t: TupleType) -> ProperType:
563567
# When given two fixed-length tuples:
564568
# * If they have the same length, join their subtypes item-wise:

mypy/meet.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
ParamSpecType,
3333
PartialType,
3434
ProperType,
35+
TupleGetterType,
3536
TupleType,
3637
Type,
3738
TypeAliasType,
@@ -1039,6 +1040,9 @@ def meet_tuples(self, s: TupleType, t: TupleType) -> list[Type] | None:
10391040
items.append(self.meet(fi, vi))
10401041
return items
10411042

1043+
def visit_tuplegetter_type(self, t: TupleGetterType) -> ProperType:
1044+
raise NotImplementedError
1045+
10421046
def visit_tuple_type(self, t: TupleType) -> ProperType:
10431047
if isinstance(self.s, TupleType):
10441048
items = self.meet_tuples(self.s, t)

mypy/messages.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
ParamSpecType,
8585
PartialType,
8686
ProperType,
87+
TupleGetterType,
8788
TupleType,
8889
Type,
8990
TypeAliasType,
@@ -2664,6 +2665,8 @@ def format_literal_value(typ: LiteralType) -> str:
26642665
else:
26652666
# TODO: better disambiguate ParamSpec name clashes.
26662667
return typ.name_with_suffix()
2668+
elif isinstance(typ, TupleGetterType):
2669+
return f"TupleGetterType[{typ.typ}]"
26672670
elif isinstance(typ, TupleType):
26682671
# Prefer the name of the fallback class (if not tuple), as it's more informative.
26692672
if typ.partial_fallback.type.fullname != "builtins.tuple":

0 commit comments

Comments
 (0)