@@ -23,6 +23,13 @@ Protocol: _SpecialForm = ...
23
23
Callable : _SpecialForm = ...
24
24
Type : _SpecialForm = ...
25
25
ClassVar : _SpecialForm = ...
26
+ if sys .version_info >= (3 , 8 ):
27
+ Final : _SpecialForm = ...
28
+ _F = TypeVar ('_F' , bound = Callable [..., Any ])
29
+ def final (f : _F ) -> _F : ...
30
+ Literal : _SpecialForm = ...
31
+ # TypedDict is a (non-subscriptable) special form.
32
+ TypedDict : object = ...
26
33
27
34
class GenericMeta (type ): ...
28
35
@@ -67,34 +74,34 @@ _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
67
74
_TC = TypeVar ('_TC' , bound = Type [object ])
68
75
_C = TypeVar ("_C" , bound = Callable )
69
76
70
- def runtime (cls : _TC ) -> _TC : ...
77
+ def runtime_checkable (cls : _TC ) -> _TC : ...
71
78
72
- @runtime
79
+ @runtime_checkable
73
80
class SupportsInt (Protocol , metaclass = ABCMeta ):
74
81
@abstractmethod
75
82
def __int__ (self ) -> int : ...
76
83
77
- @runtime
84
+ @runtime_checkable
78
85
class SupportsFloat (Protocol , metaclass = ABCMeta ):
79
86
@abstractmethod
80
87
def __float__ (self ) -> float : ...
81
88
82
- @runtime
89
+ @runtime_checkable
83
90
class SupportsComplex (Protocol , metaclass = ABCMeta ):
84
91
@abstractmethod
85
92
def __complex__ (self ) -> complex : ...
86
93
87
- @runtime
94
+ @runtime_checkable
88
95
class SupportsBytes (Protocol , metaclass = ABCMeta ):
89
96
@abstractmethod
90
97
def __bytes__ (self ) -> bytes : ...
91
98
92
- @runtime
99
+ @runtime_checkable
93
100
class SupportsAbs (Protocol [_T_co ]):
94
101
@abstractmethod
95
102
def __abs__ (self ) -> _T_co : ...
96
103
97
- @runtime
104
+ @runtime_checkable
98
105
class SupportsRound (Protocol [_T_co ]):
99
106
@overload
100
107
@abstractmethod
@@ -103,30 +110,30 @@ class SupportsRound(Protocol[_T_co]):
103
110
@abstractmethod
104
111
def __round__ (self , ndigits : int ) -> _T_co : ...
105
112
106
- @runtime
113
+ @runtime_checkable
107
114
class Reversible (Protocol [_T_co ]):
108
115
@abstractmethod
109
116
def __reversed__ (self ) -> Iterator [_T_co ]: ...
110
117
111
- @runtime
118
+ @runtime_checkable
112
119
class Sized (Protocol , metaclass = ABCMeta ):
113
120
@abstractmethod
114
121
def __len__ (self ) -> int : ...
115
122
116
- @runtime
123
+ @runtime_checkable
117
124
class Hashable (Protocol , metaclass = ABCMeta ):
118
125
# TODO: This is special, in that a subclass of a hashable class may not be hashable
119
126
# (for example, list vs. object). It's not obvious how to represent this. This class
120
127
# is currently mostly useless for static checking.
121
128
@abstractmethod
122
129
def __hash__ (self ) -> int : ...
123
130
124
- @runtime
131
+ @runtime_checkable
125
132
class Iterable (Protocol [_T_co ]):
126
133
@abstractmethod
127
134
def __iter__ (self ) -> Iterator [_T_co ]: ...
128
135
129
- @runtime
136
+ @runtime_checkable
130
137
class Iterator (Iterable [_T_co ], Protocol [_T_co ]):
131
138
@abstractmethod
132
139
def __next__ (self ) -> _T_co : ...
@@ -162,7 +169,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
162
169
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
163
170
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
164
171
165
- @runtime
172
+ @runtime_checkable
166
173
class Awaitable (Protocol [_T_co ]):
167
174
@abstractmethod
168
175
def __await__ (self ) -> Generator [Any , None , _T_co ]: ...
@@ -193,12 +200,12 @@ class Coroutine(Awaitable[_V_co], Generic[_T_co, _T_contra, _V_co]):
193
200
class AwaitableGenerator (Awaitable [_V_co ], Generator [_T_co , _T_contra , _V_co ],
194
201
Generic [_T_co , _T_contra , _V_co , _S ], metaclass = ABCMeta ): ...
195
202
196
- @runtime
203
+ @runtime_checkable
197
204
class AsyncIterable (Protocol [_T_co ]):
198
205
@abstractmethod
199
206
def __aiter__ (self ) -> AsyncIterator [_T_co ]: ...
200
207
201
- @runtime
208
+ @runtime_checkable
202
209
class AsyncIterator (AsyncIterable [_T_co ],
203
210
Protocol [_T_co ]):
204
211
@abstractmethod
@@ -232,22 +239,22 @@ if sys.version_info >= (3, 6):
232
239
@property
233
240
def ag_running (self ) -> bool : ...
234
241
235
- @runtime
242
+ @runtime_checkable
236
243
class Container (Protocol [_T_co ]):
237
244
@abstractmethod
238
245
def __contains__ (self , __x : object ) -> bool : ...
239
246
240
247
241
248
if sys .version_info >= (3 , 6 ):
242
- @runtime
249
+ @runtime_checkable
243
250
class Collection (Iterable [_T_co ], Container [_T_co ], Protocol [_T_co ]):
244
251
# Implement Sized (but don't have it as a base class).
245
252
@abstractmethod
246
253
def __len__ (self ) -> int : ...
247
254
248
255
_Collection = Collection
249
256
else :
250
- @runtime
257
+ @runtime_checkable
251
258
class _Collection (Iterable [_T_co ], Container [_T_co ], Protocol [_T_co ]):
252
259
# Implement Sized (but don't have it as a base class).
253
260
@abstractmethod
@@ -359,15 +366,15 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
359
366
def __contains__ (self , o : object ) -> bool : ...
360
367
def __iter__ (self ) -> Iterator [_VT_co ]: ...
361
368
362
- @runtime
369
+ @runtime_checkable
363
370
class ContextManager (Protocol [_T_co ]):
364
371
def __enter__ (self ) -> _T_co : ...
365
372
def __exit__ (self , __exc_type : Optional [Type [BaseException ]],
366
373
__exc_value : Optional [BaseException ],
367
374
__traceback : Optional [TracebackType ]) -> Optional [bool ]: ...
368
375
369
376
if sys .version_info >= (3 , 5 ):
370
- @runtime
377
+ @runtime_checkable
371
378
class AsyncContextManager (Protocol [_T_co ]):
372
379
def __aenter__ (self ) -> Awaitable [_T_co ]: ...
373
380
def __aexit__ (self , exc_type : Optional [Type [BaseException ]],
@@ -600,6 +607,17 @@ class NamedTuple(tuple):
600
607
def _asdict (self ) -> collections .OrderedDict [str , Any ]: ...
601
608
def _replace (self : _T , ** kwargs : Any ) -> _T : ...
602
609
610
+ # Internal mypy fallback type for all typed dicts (does not exist at runtime)
611
+ class _TypedDict (Mapping [str , object ], metaclass = ABCMeta ):
612
+ def copy (self : _T ) -> _T : ...
613
+ # Using NoReturn so that only calls using mypy plugin hook that specialize the signature
614
+ # can go through.
615
+ def setdefault (self , k : NoReturn , default : object ) -> object : ...
616
+ # Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
617
+ def pop (self , k : NoReturn , default : _T = ...) -> object : ...
618
+ def update (self : _T , __m : _T ) -> None : ...
619
+ def __delitem__ (self , k : NoReturn ) -> None : ...
620
+
603
621
def NewType (name : str , tp : Type [_T ]) -> Type [_T ]: ...
604
622
605
623
# This itself is only available during type checking
0 commit comments