-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathprotocol.html
More file actions
246 lines (220 loc) · 10.9 KB
/
Copy pathprotocol.html
File metadata and controls
246 lines (220 loc) · 10.9 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<html>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<head>
<title>
leontrolski - stdlib Protocol types
</title>
<style>
body {margin: 5% auto; background: #fff7f7; color: #444444; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.8; max-width: 63%;}
@media screen and (max-width: 800px) {body {font-size: 14px; line-height: 1.4; max-width: 90%;}}
pre {width: 100%; border-top: 3px solid gray; border-bottom: 3px solid gray;}
a {border-bottom: 1px solid #444444; color: #444444; text-decoration: none; text-shadow: 0 1px 0 #ffffff; }
a:hover {border-bottom: 0;}
.inline {background: #b3b2b226; padding-left: 0.3em; padding-right: 0.3em; white-space: nowrap;}
blockquote {font-style: italic;color:black;background-color:#f2f2f2;padding:2em;}
details {border-bottom:solid 5px gray;}
</style>
<link href="https://unpkg.com/prism-themes@1.9.0/themes/prism-darcula.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/plugins/autoloader/prism-autoloader.min.js"></script>
</head>
<body>
<a href="index.html">
<img src="pic.png" style="height:2em">
⇦
</a>
<p><i>2025-03-20</i></p>
<h1><code>stdlib</code> Protocol types</h1>
<p>
The Python stdlib has a load of useful <code>Protocol</code> types in <code>typing</code>, for example: <code>from typing import Iterable</code>.
</p>
<p>
I can never remember what's what. Here is a condensed version of <a href="https://github.com/python/mypy/blob/cd422e098efcf2df82e4c42070e3dcc8180b53e0/mypy/typeshed/stdlib/typing.pyi#L583">this file</a> (as of 2025-01-01) from the typeshed, in approximately the order I care about them in.
</p>
<p>
<code>TypeVar</code>s ending with <code>_co</code> are <a href="covariance.html">covariant</a>, <code>TypeVar</code>s ending with <code>_con</code> are contravariant.
</p>
<pre class="language-python"><code>class Sized(Protocol):
def __len__(self) -> int: ...
class Hashable(Protocol):
def __hash__(self) -> int: ...
class Container(Protocol[T_co]):
def __contains__(self, x: object, /) -> bool: ...
class Iterable(Protocol[T_co]):
def __iter__(self) -> Iterator[T_co]: ...
class Iterator(Iterable[T_co], Protocol[T_co]):
def __next__(self) -> T_co: ...
def __iter__(self) -> Iterator[T_co]: ...
class Collection(Iterable[T_co], Container[T_co], Protocol[T_co]):
def __len__(self) -> int: ...
class Reversible(Iterable[T_co], Protocol[T_co]):
def __reversed__(self) -> Iterator[T_co]: ...
class Sequence(Reversible[T_co], Collection[T_co]):
@overload
def __getitem__(self, index: int) -> T_co: ...
@overload
def __getitem__(self, index: slice) -> Sequence[T_co]: ...
def index(self, value: Any, start: int = 0, stop: int = ...) -> int: ...
def count(self, value: Any) -> int: ...
def __contains__(self, value: object) -> bool: ...
def __iter__(self) -> Iterator[T_co]: ...
def __reversed__(self) -> Iterator[T_co]: ...
class Generator(Iterator[U_co], Protocol[U_co, C_con, R_co]):
def __next__(self) -> U_co: ...
def send(self, value: C_con, /) -> U_co: ...
@overload
def throw(self, typ: type[BaseException], val: BaseException | object = None, tb: TracebackType | None = None, /) -> U_co: ...
@overload
def throw(self, typ: BaseException, val: None = None, tb: TracebackType | None = None, /) -> U_co: ...
def close(self) -> R_co | None: ...
def __iter__(self) -> Generator[U_co, C_con, R_co]: ...
class MutableSequence(Sequence[T]):
def insert(self, index: int, value: T) -> None: ...
@overload
def __getitem__(self, index: int) -> T: ...
@overload
def __getitem__(self, index: slice) -> MutableSequence[T]: ...
@overload
def __setitem__(self, index: int, value: T) -> None: ...
@overload
def __setitem__(self, index: slice, value: Iterable[T]) -> None: ...
@overload
def __delitem__(self, index: int) -> None: ...
@overload
def __delitem__(self, index: slice) -> None: ...
def append(self, value: T) -> None: ...
def clear(self) -> None: ...
def extend(self, values: Iterable[T]) -> None: ...
def reverse(self) -> None: ...
def pop(self, index: int = -1) -> T: ...
def remove(self, value: T) -> None: ...
def __iadd__(self, values: Iterable[T]) -> Self: ...
class AbstractSet(Collection[T_co]):
def __contains__(self, x: object) -> bool: ...
def _hash(self) -> int: ...
def __le__(self, other: AbstractSet[Any]) -> bool: ...
def __lt__(self, other: AbstractSet[Any]) -> bool: ...
def __gt__(self, other: AbstractSet[Any]) -> bool: ...
def __ge__(self, other: AbstractSet[Any]) -> bool: ...
def __and__(self, other: AbstractSet[Any]) -> AbstractSet[T_co]: ...
def __or__(self, other: AbstractSet[T]) -> AbstractSet[T_co | T]: ...
def __sub__(self, other: AbstractSet[Any]) -> AbstractSet[T_co]: ...
def __xor__(self, other: AbstractSet[T]) -> AbstractSet[T_co | T]: ...
def __eq__(self, other: object) -> bool: ...
def isdisjoint(self, other: Iterable[Any]) -> bool: ...
class MutableSet(AbstractSet[T]):
def add(self, value: T) -> None: ...
def discard(self, value: T) -> None: ...
def clear(self) -> None: ...
def pop(self) -> T: ...
def remove(self, value: T) -> None: ...
def __ior__(self, it: AbstractSet[T]) -> Self: ...
def __iand__(self, it: AbstractSet[Any]) -> Self: ...
def __ixor__(self, it: AbstractSet[T]) -> Self: ...
def __isub__(self, it: AbstractSet[Any]) -> Self: ...
class Mapping(Collection[K], Generic[K, V_co]):
def __getitem__(self, key: K, /) -> V_co: ...
@overload
def get(self, key: K, /) -> V_co | None: ...
@overload
def get(self, key: K, /, default: V_co | T) -> V_co | T: ...
def items(self) -> ItemsView[K, V_co]: ...
def keys(self) -> KeysView[K]: ...
def values(self) -> ValuesView[V_co]: ...
def __contains__(self, key: object, /) -> bool: ...
def __eq__(self, other: object, /) -> bool: ...
class MutableMapping(Mapping[K, V]):
def __setitem__(self, key: K, value: V, /) -> None: ...
def __delitem__(self, key: K, /) -> None: ...
def clear(self) -> None: ...
@overload
def pop(self, key: K, /) -> V: ...
@overload
def pop(self, key: K, /, default: V) -> V: ...
@overload
def pop(self, key: K, /, default: T) -> V | T: ...
def popitem(self) -> tuple[K, V]: ...
@overload
def setdefault(self: MutableMapping[K, T | None], key: K, default: None = None, /) -> T | None: ...
@overload
def setdefault(self, key: K, default: V, /) -> V: ...
@overload
def update(self, m: typeshed.SupportsKeysAndGetItem[K, V], /, **kwargs: V) -> None: ...
@overload
def update(self, m: Iterable[tuple[K, V]], /, **kwargs: V) -> None: ...
@overload
def update(self, **kwargs: V) -> None: ...
class SupportsInt(Protocol):
def __int__(self) -> int: ...
class SupportsFloat(Protocol):
def __float__(self) -> float: ...
class SupportsComplex(Protocol):
def __complex__(self) -> complex: ...
class SupportsBytes(Protocol):
def __bytes__(self) -> bytes: ...
class SupportsIndex(Protocol):
def __index__(self) -> int: ...
class SupportsAbs(Protocol[T_co]):
def __abs__(self) -> T_co: ...
class SupportsRound(Protocol[T_co]):
@overload
def __round__(self) -> int: ...
@overload
def __round__(self, ndigits: int, /) -> T_co: ...
class Awaitable(Protocol[T_co]):
def __await__(self) -> Generator[Any, Any, T_co]: ...
class Coroutine(Awaitable[R_co], Generic[U_co, C_con, R_co]):
__name__: str
__qualname__: str
def send(self, value: C_con, /) -> U_co: ...
@overload
def throw(self, typ: type[BaseException], val: BaseException | object = None, tb: TracebackType | None = None, /) -> U_co: ...
@overload
def throw(self, typ: BaseException, val: None = None, tb: TracebackType | None = None, /) -> U_co: ...
def close(self) -> None: ...
class AsyncIterable(Protocol[T_co]):
def __aiter__(self) -> AsyncIterator[T_co]: ...
class AsyncIterator(AsyncIterable[T_co], Protocol[T_co]):
def __anext__(self) -> Awaitable[T_co]: ...
def __aiter__(self) -> AsyncIterator[T_co]: ...
class AsyncGenerator(AsyncIterator[U_co], Protocol[U_co, C_con]):
def __anext__(self) -> Coroutine[Any, Any, U_co]: ...
def asend(self, value: C_con, /) -> Coroutine[Any, Any, U_co]: ...
@overload
def athrow(self, typ: type[BaseException], val: BaseException | object = None, tb: TracebackType | None = None, /) -> Coroutine[Any, Any, U_co]: ...
@overload
def athrow(self, typ: BaseException, val: None = None, tb: TracebackType | None = None, /) -> Coroutine[Any, Any, U_co]: ...
def aclose(self) -> Coroutine[Any, Any, None]: ...
class MappingView(Sized):
def __init__(self, mapping: Mapping[Any, Any]) -> None: ...
def __len__(self) -> int: ...
class ItemsView(MappingView, AbstractSet[tuple[K_co, V_co]], Generic[K_co, V_co]):
def __init__(self, mapping: Mapping[K_co, V_co]) -> None: ...
def __and__(self, other: Iterable[Any]) -> set[tuple[K_co, V_co]]: ...
def __rand__(self, other: Iterable[T]) -> set[T]: ...
def __contains__(self, item: object) -> bool: ...
def __iter__(self) -> Iterator[tuple[K_co, V_co]]: ...
def __or__(self, other: Iterable[T]) -> set[tuple[K_co, V_co] | T]: ...
def __ror__(self, other: Iterable[T]) -> set[tuple[K_co, V_co] | T]: ...
def __sub__(self, other: Iterable[Any]) -> set[tuple[K_co, V_co]]: ...
def __rsub__(self, other: Iterable[T]) -> set[T]: ...
def __xor__(self, other: Iterable[T]) -> set[tuple[K_co, V_co] | T]: ...
def __rxor__(self, other: Iterable[T]) -> set[tuple[K_co, V_co] | T]: ...
class KeysView(MappingView, AbstractSet[K_co]):
def __init__(self, mapping: Mapping[K_co, Any]) -> None: ...
def __and__(self, other: Iterable[Any]) -> set[K_co]: ...
def __rand__(self, other: Iterable[T]) -> set[T]: ...
def __contains__(self, key: object) -> bool: ...
def __iter__(self) -> Iterator[K_co]: ...
def __or__(self, other: Iterable[T]) -> set[K_co | T]: ...
def __ror__(self, other: Iterable[T]) -> set[K_co | T]: ...
def __sub__(self, other: Iterable[Any]) -> set[K_co]: ...
def __rsub__(self, other: Iterable[T]) -> set[T]: ...
def __xor__(self, other: Iterable[T]) -> set[K_co | T]: ...
def __rxor__(self, other: Iterable[T]) -> set[K_co | T]: ...
class ValuesView(MappingView, Collection[V_co]):
def __init__(self, mapping: Mapping[Any, V_co]) -> None: ...
def __contains__(self, value: object) -> bool: ...
def __iter__(self) -> Iterator[V_co]: ...</code></pre>
</body>
</html>