-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathtuples_type_compat.py
More file actions
188 lines (129 loc) · 5.55 KB
/
tuples_type_compat.py
File metadata and controls
188 lines (129 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Tests type compatibility rules for tuples.
"""
# Specification: https://typing.readthedocs.io/en/latest/spec/tuples.html#type-compatibility-rules
# > Because tuple contents are immutable, the element types of a tuple are covariant.
from typing import Any, Iterable, Never, Sequence, TypeAlias, TypeVar, assert_type
def func1(t1: tuple[float, complex], t2: tuple[int, int]):
v1: tuple[float, complex] = t2 # OK
v2: tuple[int, int] = t1 # E
# > A homogeneous tuple of arbitrary length is equivalent
# > to a union of tuples of different lengths.
def func2(t1: tuple[int], t2: tuple[int, *tuple[int, ...]], t3: tuple[int, ...]):
v1: tuple[int, ...]
v1 = t1 # OK
v1 = t2 # OK
v2: tuple[int, *tuple[int, ...]]
v2 = t1 # OK
v2 = t3 # E
v3: tuple[int]
v3 = t2 # E
v3 = t3 # E
# > The type ``tuple[Any, ...]`` is bidirectionally compatible with any tuple::
def func3(t1: tuple[int], t2: tuple[int, ...], t3: tuple[Any, ...]):
v1: tuple[int, ...] = t1 # OK
v2: tuple[Any, ...] = t1 # OK
v3: tuple[int] = t2 # E
v4: tuple[Any, ...] = t2 # OK
v5: tuple[float, float] = t3 # OK
v6: tuple[int, *tuple[str, ...]] = t3 # OK
from missing_module import SomeType # type: ignore
def func4(
untyped_iter: Iterable[Any],
some_type_tuple: tuple[SomeType, ...],
any_tuple: tuple[Any, ...],
int_tuple: tuple[int, ...],
):
a: tuple[int, int] = tuple(untyped_iter) # OK
b: tuple[int, int] = some_type_tuple # OK
c: tuple[int, int] = any_tuple # OK
d: tuple[int, int] = int_tuple # E
# > The length of a tuple at runtime is immutable, so it is safe for type
# > checkers to use length checks to narrow the type of a tuple
# NOTE: This type narrowing functionality is optional, not mandated.
Func5Input: TypeAlias = tuple[int] | tuple[str, str] | tuple[int, *tuple[str, ...], int]
def func5(val: Func5Input):
if len(val) == 1:
# Type can be narrowed to tuple[int].
assert_type(val, tuple[int]) # E[func5_1]
assert_type(val, Func5Input) # E[func5_1]
if len(val) == 2:
# Type can be narrowed to tuple[str, str] | tuple[int, int].
assert_type(val, tuple[str, str] | tuple[int, int]) # E[func5_2]
assert_type(val, Func5Input) # E[func5_2]
if len(val) == 3:
# Type can be narrowed to tuple[int, str, int].
assert_type(val, tuple[int, str, int]) # E[func5_3]
assert_type(val, Func5Input) # E[func5_3]
# > This property may also be used to safely narrow tuple types within a match
# > statement that uses sequence patterns.
# NOTE: This type narrowing functionality is optional, not mandated.
Func6Input: TypeAlias = tuple[int] | tuple[str, str] | tuple[int, *tuple[str, ...], int]
def func6(val: Func6Input):
match val:
case (x,):
# Type may be narrowed to tuple[int].
assert_type(val, tuple[int]) # E[func6_1]
assert_type(val, Func6Input) # E[func6_1]
case (x, y):
# Type may be narrowed to tuple[str, str] | tuple[int, int].
assert_type(val, tuple[str, str] | tuple[int, int]) # E[func6_2]
assert_type(val, Func6Input) # E[func6_2]
case (x, y, z):
# Type may be narrowed to tuple[int, str, int].
assert_type(val, tuple[int, str, int]) # E[func6_3]
assert_type(val, Func6Input) # E[func6_3]
# > Type checkers may safely use this equivalency rule (tuple expansion)
# > when narrowing tuple types
# NOTE: This type narrowing functionality is optional, not mandated.
Func7Input: TypeAlias = tuple[int | str, int | str]
def func7(subj: Func7Input):
match subj:
case x, str():
assert_type(subj, tuple[int | str, str]) # E[func7_1]
assert_type(subj, Func7Input) # E[func7_1]
case y:
assert_type(subj, tuple[int | str, int]) # E[func7_2]
assert_type(subj, Func7Input) # E[func7_2]
# > The tuple class derives from Sequence[T_co] where ``T_co`` is a covariant
# (non-variadic) type variable. The specialized type of ``T_co`` should be
# computed by a type checker as a a supertype of all element types.
# > A zero-length tuple (``tuple[()]``) is a subtype of ``Sequence[Never]``.
T = TypeVar("T")
def test_seq(x: Sequence[T]) -> Sequence[T]:
return x
def func8(
t1: tuple[complex, list[int]], t2: tuple[int, *tuple[str, ...]], t3: tuple[()]
):
assert_type(
test_seq(t1), Sequence[complex | list[int]]
) # Could be Sequence[object]
assert_type(test_seq(t2), Sequence[int | str]) # Could be Sequence[object]
assert_type(test_seq(t3), Sequence[Never])
t1: tuple[int, *tuple[str]] = (1, "") # OK
t1 = (1, "", "") # E
t2: tuple[int, *tuple[str, ...]] = (1,) # OK
t2 = (1, "") # OK
t2 = (1, "", "") # OK
t2 = (1, 1, "") # E
t2 = (1, "", 1) # E
t3: tuple[int, *tuple[str, ...], int] = (1, 2) # OK
t3 = (1, "", 2) # OK
t3 = (1, "", "", 2) # OK
t3 = (1, "", "") # E
t3 = (1, "", "", 1.2) # E
t4: tuple[*tuple[str, ...], int] = (1,) # OK
t4 = ("", 1) # OK
t4 = ("", "", 1) # OK
t4 = (1, "", 1) # E
t4 = ("", "", 1.2) # E
def func9(a: tuple[str, str]):
t1: tuple[str, str, *tuple[int, ...]] = a # OK
t2: tuple[str, str, *tuple[int]] = a # E
t3: tuple[str, *tuple[str, ...]] = a # OK
t4: tuple[str, str, *tuple[str, ...]] = a # OK
t5: tuple[str, str, str, *tuple[str, ...]] = a # E
t6: tuple[str, *tuple[int, ...], str] = a # OK
t7: tuple[*tuple[str, ...], str] = a # OK
t8: tuple[*tuple[str, ...], str] = a # OK
t9: tuple[*tuple[str, ...], str, str, str] = a # E