@@ -19,6 +19,7 @@ class _SpecialForm:
1919
2020Tuple : _SpecialForm = ...
2121Generic : _SpecialForm = ...
22+ Protocol : _SpecialForm = ...
2223Callable : _SpecialForm = ...
2324Type : _SpecialForm = ...
2425ClassVar : _SpecialForm = ...
@@ -64,51 +65,65 @@ _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers.
6465_KT_co = TypeVar ('_KT_co' , covariant = True ) # Key type covariant containers.
6566_VT_co = TypeVar ('_VT_co' , covariant = True ) # Value type covariant containers.
6667_T_contra = TypeVar ('_T_contra' , contravariant = True ) # Ditto contravariant.
68+ _TC = TypeVar ('_TC' , bound = Type [object ])
6769
68- class SupportsInt (metaclass = ABCMeta ):
70+ def runtime (cls : _TC ) -> _TC : ...
71+
72+ @runtime
73+ class SupportsInt (Protocol , metaclass = ABCMeta ):
6974 @abstractmethod
7075 def __int__ (self ) -> int : ...
7176
72- class SupportsFloat (metaclass = ABCMeta ):
77+ @runtime
78+ class SupportsFloat (Protocol , metaclass = ABCMeta ):
7379 @abstractmethod
7480 def __float__ (self ) -> float : ...
7581
76- class SupportsComplex (metaclass = ABCMeta ):
82+ @runtime
83+ class SupportsComplex (Protocol , metaclass = ABCMeta ):
7784 @abstractmethod
7885 def __complex__ (self ) -> complex : ...
7986
80- class SupportsBytes (metaclass = ABCMeta ):
87+ @runtime
88+ class SupportsBytes (Protocol , metaclass = ABCMeta ):
8189 @abstractmethod
8290 def __bytes__ (self ) -> bytes : ...
8391
84- class SupportsAbs (Generic [_T ]):
92+ @runtime
93+ class SupportsAbs (Protocol [_T_co ]):
8594 @abstractmethod
86- def __abs__ (self ) -> _T : ...
95+ def __abs__ (self ) -> _T_co : ...
8796
88- class SupportsRound (Generic [_T ]):
97+ @runtime
98+ class SupportsRound (Protocol [_T_co ]):
8999 @abstractmethod
90- def __round__ (self , ndigits : int = ...) -> _T : ...
100+ def __round__ (self , ndigits : int = ...) -> _T_co : ...
91101
92- class Reversible (Generic [_T_co ]):
102+ @runtime
103+ class Reversible (Protocol [_T_co ]):
93104 @abstractmethod
94105 def __reversed__ (self ) -> Iterator [_T_co ]: ...
95106
96- class Sized (metaclass = ABCMeta ):
107+ @runtime
108+ class Sized (Protocol , metaclass = ABCMeta ):
97109 @abstractmethod
98110 def __len__ (self ) -> int : ...
99111
100- class Hashable (metaclass = ABCMeta ):
112+ @runtime
113+ class Hashable (Protocol , metaclass = ABCMeta ):
101114 # TODO: This is special, in that a subclass of a hashable class may not be hashable
102115 # (for example, list vs. object). It's not obvious how to represent this. This class
103116 # is currently mostly useless for static checking.
104117 @abstractmethod
105118 def __hash__ (self ) -> int : ...
106119
107- class Iterable (Generic [_T_co ]):
120+ @runtime
121+ class Iterable (Protocol [_T_co ]):
108122 @abstractmethod
109123 def __iter__ (self ) -> Iterator [_T_co ]: ...
110124
111- class Iterator (Iterable [_T_co ], Generic [_T_co ]):
125+ @runtime
126+ class Iterator (Iterable [_T_co ], Protocol [_T_co ]):
112127 @abstractmethod
113128 def __next__ (self ) -> _T_co : ...
114129 def __iter__ (self ) -> 'Iterator[_T_co]' : ...
@@ -139,7 +154,8 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
139154# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
140155# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
141156
142- class Awaitable (Generic [_T_co ]):
157+ @runtime
158+ class Awaitable (Protocol [_T_co ]):
143159 @abstractmethod
144160 def __await__ (self ) -> Generator [Any , None , _T_co ]: ...
145161
@@ -161,12 +177,14 @@ class AwaitableGenerator(Generator[_T_co, _T_contra, _V_co], Awaitable[_V_co],
161177 Generic [_T_co , _T_contra , _V_co , _S ]):
162178 pass
163179
164- class AsyncIterable (Generic [_T_co ]):
180+ @runtime
181+ class AsyncIterable (Protocol [_T_co ]):
165182 @abstractmethod
166183 def __aiter__ (self ) -> 'AsyncIterator[_T_co]' : ...
167184
185+ @runtime
168186class AsyncIterator (AsyncIterable [_T_co ],
169- Generic [_T_co ]):
187+ Protocol [_T_co ]):
170188 @abstractmethod
171189 def __anext__ (self ) -> Awaitable [_T_co ]: ...
172190 def __aiter__ (self ) -> 'AsyncIterator[_T_co]' : ...
@@ -194,16 +212,19 @@ if sys.version_info >= (3, 6):
194212 ag_frame = ... # type: FrameType
195213 ag_running = ... # type: bool
196214
197- class Container (Generic [_T_co ]):
215+ @runtime
216+ class Container (Protocol [_T_co ]):
198217 @abstractmethod
199218 def __contains__ (self , x : object ) -> bool : ...
200219
201220
202221if 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 ]): ...
204224 _Collection = Collection
205225else :
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 ]): ...
207228
208229class Sequence (_Collection [_T_co ], Reversible [_T_co ], Generic [_T_co ]):
209230 @overload
@@ -289,14 +310,16 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
289310 def __contains__ (self , o : object ) -> bool : ...
290311 def __iter__ (self ) -> Iterator [_VT_co ]: ...
291312
292- class ContextManager (Generic [_T_co ]):
313+ @runtime
314+ class ContextManager (Protocol [_T_co ]):
293315 def __enter__ (self ) -> _T_co : ...
294316 def __exit__ (self , exc_type : Optional [Type [BaseException ]],
295317 exc_value : Optional [BaseException ],
296318 traceback : Optional [TracebackType ]) -> Optional [bool ]: ...
297319
298320if sys .version_info >= (3 , 5 ):
299- class AsyncContextManager (Generic [_T_co ]):
321+ @runtime
322+ class AsyncContextManager (Protocol [_T_co ]):
300323 def __aenter__ (self ) -> Awaitable [_T_co ]: ...
301324 def __aexit__ (self , exc_type : Optional [Type [BaseException ]],
302325 exc_value : Optional [BaseException ],
0 commit comments