@@ -19,6 +19,7 @@ class _SpecialForm:
19
19
20
20
Tuple : _SpecialForm = ...
21
21
Generic : _SpecialForm = ...
22
+ Protocol : _SpecialForm = ...
22
23
Callable : _SpecialForm = ...
23
24
Type : _SpecialForm = ...
24
25
ClassVar : _SpecialForm = ...
@@ -64,51 +65,65 @@ _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers.
64
65
_KT_co = TypeVar ('_KT_co' , covariant = True ) # Key type covariant containers.
65
66
_VT_co = TypeVar ('_VT_co' , covariant = True ) # Value type covariant containers.
66
67
_T_contra = TypeVar ('_T_contra' , contravariant = True ) # Ditto contravariant.
68
+ _TC = TypeVar ('_TC' , bound = Type [object ])
67
69
68
- class SupportsInt (metaclass = ABCMeta ):
70
+ def runtime (cls : _TC ) -> _TC : ...
71
+
72
+ @runtime
73
+ class SupportsInt (Protocol , metaclass = ABCMeta ):
69
74
@abstractmethod
70
75
def __int__ (self ) -> int : ...
71
76
72
- class SupportsFloat (metaclass = ABCMeta ):
77
+ @runtime
78
+ class SupportsFloat (Protocol , metaclass = ABCMeta ):
73
79
@abstractmethod
74
80
def __float__ (self ) -> float : ...
75
81
76
- class SupportsComplex (metaclass = ABCMeta ):
82
+ @runtime
83
+ class SupportsComplex (Protocol , metaclass = ABCMeta ):
77
84
@abstractmethod
78
85
def __complex__ (self ) -> complex : ...
79
86
80
- class SupportsBytes (metaclass = ABCMeta ):
87
+ @runtime
88
+ class SupportsBytes (Protocol , metaclass = ABCMeta ):
81
89
@abstractmethod
82
90
def __bytes__ (self ) -> bytes : ...
83
91
84
- class SupportsAbs (Generic [_T ]):
92
+ @runtime
93
+ class SupportsAbs (Protocol [_T_co ]):
85
94
@abstractmethod
86
- def __abs__ (self ) -> _T : ...
95
+ def __abs__ (self ) -> _T_co : ...
87
96
88
- class SupportsRound (Generic [_T ]):
97
+ @runtime
98
+ class SupportsRound (Protocol [_T_co ]):
89
99
@abstractmethod
90
- def __round__ (self , ndigits : int = ...) -> _T : ...
100
+ def __round__ (self , ndigits : int = ...) -> _T_co : ...
91
101
92
- class Reversible (Generic [_T_co ]):
102
+ @runtime
103
+ class Reversible (Protocol [_T_co ]):
93
104
@abstractmethod
94
105
def __reversed__ (self ) -> Iterator [_T_co ]: ...
95
106
96
- class Sized (metaclass = ABCMeta ):
107
+ @runtime
108
+ class Sized (Protocol , metaclass = ABCMeta ):
97
109
@abstractmethod
98
110
def __len__ (self ) -> int : ...
99
111
100
- class Hashable (metaclass = ABCMeta ):
112
+ @runtime
113
+ class Hashable (Protocol , metaclass = ABCMeta ):
101
114
# TODO: This is special, in that a subclass of a hashable class may not be hashable
102
115
# (for example, list vs. object). It's not obvious how to represent this. This class
103
116
# is currently mostly useless for static checking.
104
117
@abstractmethod
105
118
def __hash__ (self ) -> int : ...
106
119
107
- class Iterable (Generic [_T_co ]):
120
+ @runtime
121
+ class Iterable (Protocol [_T_co ]):
108
122
@abstractmethod
109
123
def __iter__ (self ) -> Iterator [_T_co ]: ...
110
124
111
- class Iterator (Iterable [_T_co ], Generic [_T_co ]):
125
+ @runtime
126
+ class Iterator (Iterable [_T_co ], Protocol [_T_co ]):
112
127
@abstractmethod
113
128
def __next__ (self ) -> _T_co : ...
114
129
def __iter__ (self ) -> 'Iterator[_T_co]' : ...
@@ -139,7 +154,8 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
139
154
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
140
155
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
141
156
142
- class Awaitable (Generic [_T_co ]):
157
+ @runtime
158
+ class Awaitable (Protocol [_T_co ]):
143
159
@abstractmethod
144
160
def __await__ (self ) -> Generator [Any , None , _T_co ]: ...
145
161
@@ -161,12 +177,14 @@ class AwaitableGenerator(Generator[_T_co, _T_contra, _V_co], Awaitable[_V_co],
161
177
Generic [_T_co , _T_contra , _V_co , _S ]):
162
178
pass
163
179
164
- class AsyncIterable (Generic [_T_co ]):
180
+ @runtime
181
+ class AsyncIterable (Protocol [_T_co ]):
165
182
@abstractmethod
166
183
def __aiter__ (self ) -> 'AsyncIterator[_T_co]' : ...
167
184
185
+ @runtime
168
186
class AsyncIterator (AsyncIterable [_T_co ],
169
- Generic [_T_co ]):
187
+ Protocol [_T_co ]):
170
188
@abstractmethod
171
189
def __anext__ (self ) -> Awaitable [_T_co ]: ...
172
190
def __aiter__ (self ) -> 'AsyncIterator[_T_co]' : ...
@@ -194,16 +212,19 @@ if sys.version_info >= (3, 6):
194
212
ag_frame = ... # type: FrameType
195
213
ag_running = ... # type: bool
196
214
197
- class Container (Generic [_T_co ]):
215
+ @runtime
216
+ class Container (Protocol [_T_co ]):
198
217
@abstractmethod
199
218
def __contains__ (self , x : object ) -> bool : ...
200
219
201
220
202
221
if sys .version_info >= (3 , 6 ):
203
- class Collection (Sized , Iterable [_T_co ], Container [_T_co ], Generic [_T_co ]): ...
222
+ @runtime
223
+ class Collection (Sized , Iterable [_T_co ], Container [_T_co ], Protocol [_T_co ]): ...
204
224
_Collection = Collection
205
225
else :
206
- class _Collection (Sized , Iterable [_T_co ], Container [_T_co ], Generic [_T_co ]): ...
226
+ @runtime
227
+ class _Collection (Sized , Iterable [_T_co ], Container [_T_co ], Protocol [_T_co ]): ...
207
228
208
229
class Sequence (_Collection [_T_co ], Reversible [_T_co ], Generic [_T_co ]):
209
230
@overload
@@ -289,14 +310,16 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
289
310
def __contains__ (self , o : object ) -> bool : ...
290
311
def __iter__ (self ) -> Iterator [_VT_co ]: ...
291
312
292
- class ContextManager (Generic [_T_co ]):
313
+ @runtime
314
+ class ContextManager (Protocol [_T_co ]):
293
315
def __enter__ (self ) -> _T_co : ...
294
316
def __exit__ (self , exc_type : Optional [Type [BaseException ]],
295
317
exc_value : Optional [BaseException ],
296
318
traceback : Optional [TracebackType ]) -> Optional [bool ]: ...
297
319
298
320
if sys .version_info >= (3 , 5 ):
299
- class AsyncContextManager (Generic [_T_co ]):
321
+ @runtime
322
+ class AsyncContextManager (Protocol [_T_co ]):
300
323
def __aenter__ (self ) -> Awaitable [_T_co ]: ...
301
324
def __aexit__ (self , exc_type : Optional [Type [BaseException ]],
302
325
exc_value : Optional [BaseException ],
0 commit comments