@@ -23,6 +23,13 @@ Protocol: _SpecialForm = ...
2323Callable : _SpecialForm = ...
2424Type : _SpecialForm = ...
2525ClassVar : _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 = ...
2633
2734class GenericMeta (type ): ...
2835
@@ -67,34 +74,34 @@ _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
6774_TC = TypeVar ('_TC' , bound = Type [object ])
6875_C = TypeVar ("_C" , bound = Callable )
6976
70- def runtime (cls : _TC ) -> _TC : ...
77+ def runtime_checkable (cls : _TC ) -> _TC : ...
7178
72- @runtime
79+ @runtime_checkable
7380class SupportsInt (Protocol , metaclass = ABCMeta ):
7481 @abstractmethod
7582 def __int__ (self ) -> int : ...
7683
77- @runtime
84+ @runtime_checkable
7885class SupportsFloat (Protocol , metaclass = ABCMeta ):
7986 @abstractmethod
8087 def __float__ (self ) -> float : ...
8188
82- @runtime
89+ @runtime_checkable
8390class SupportsComplex (Protocol , metaclass = ABCMeta ):
8491 @abstractmethod
8592 def __complex__ (self ) -> complex : ...
8693
87- @runtime
94+ @runtime_checkable
8895class SupportsBytes (Protocol , metaclass = ABCMeta ):
8996 @abstractmethod
9097 def __bytes__ (self ) -> bytes : ...
9198
92- @runtime
99+ @runtime_checkable
93100class SupportsAbs (Protocol [_T_co ]):
94101 @abstractmethod
95102 def __abs__ (self ) -> _T_co : ...
96103
97- @runtime
104+ @runtime_checkable
98105class SupportsRound (Protocol [_T_co ]):
99106 @overload
100107 @abstractmethod
@@ -103,30 +110,30 @@ class SupportsRound(Protocol[_T_co]):
103110 @abstractmethod
104111 def __round__ (self , ndigits : int ) -> _T_co : ...
105112
106- @runtime
113+ @runtime_checkable
107114class Reversible (Protocol [_T_co ]):
108115 @abstractmethod
109116 def __reversed__ (self ) -> Iterator [_T_co ]: ...
110117
111- @runtime
118+ @runtime_checkable
112119class Sized (Protocol , metaclass = ABCMeta ):
113120 @abstractmethod
114121 def __len__ (self ) -> int : ...
115122
116- @runtime
123+ @runtime_checkable
117124class Hashable (Protocol , metaclass = ABCMeta ):
118125 # TODO: This is special, in that a subclass of a hashable class may not be hashable
119126 # (for example, list vs. object). It's not obvious how to represent this. This class
120127 # is currently mostly useless for static checking.
121128 @abstractmethod
122129 def __hash__ (self ) -> int : ...
123130
124- @runtime
131+ @runtime_checkable
125132class Iterable (Protocol [_T_co ]):
126133 @abstractmethod
127134 def __iter__ (self ) -> Iterator [_T_co ]: ...
128135
129- @runtime
136+ @runtime_checkable
130137class Iterator (Iterable [_T_co ], Protocol [_T_co ]):
131138 @abstractmethod
132139 def __next__ (self ) -> _T_co : ...
@@ -162,7 +169,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
162169# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
163170# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
164171
165- @runtime
172+ @runtime_checkable
166173class Awaitable (Protocol [_T_co ]):
167174 @abstractmethod
168175 def __await__ (self ) -> Generator [Any , None , _T_co ]: ...
@@ -193,12 +200,12 @@ class Coroutine(Awaitable[_V_co], Generic[_T_co, _T_contra, _V_co]):
193200class AwaitableGenerator (Awaitable [_V_co ], Generator [_T_co , _T_contra , _V_co ],
194201 Generic [_T_co , _T_contra , _V_co , _S ], metaclass = ABCMeta ): ...
195202
196- @runtime
203+ @runtime_checkable
197204class AsyncIterable (Protocol [_T_co ]):
198205 @abstractmethod
199206 def __aiter__ (self ) -> AsyncIterator [_T_co ]: ...
200207
201- @runtime
208+ @runtime_checkable
202209class AsyncIterator (AsyncIterable [_T_co ],
203210 Protocol [_T_co ]):
204211 @abstractmethod
@@ -232,22 +239,22 @@ if sys.version_info >= (3, 6):
232239 @property
233240 def ag_running (self ) -> bool : ...
234241
235- @runtime
242+ @runtime_checkable
236243class Container (Protocol [_T_co ]):
237244 @abstractmethod
238245 def __contains__ (self , __x : object ) -> bool : ...
239246
240247
241248if sys .version_info >= (3 , 6 ):
242- @runtime
249+ @runtime_checkable
243250 class Collection (Iterable [_T_co ], Container [_T_co ], Protocol [_T_co ]):
244251 # Implement Sized (but don't have it as a base class).
245252 @abstractmethod
246253 def __len__ (self ) -> int : ...
247254
248255 _Collection = Collection
249256else :
250- @runtime
257+ @runtime_checkable
251258 class _Collection (Iterable [_T_co ], Container [_T_co ], Protocol [_T_co ]):
252259 # Implement Sized (but don't have it as a base class).
253260 @abstractmethod
@@ -359,15 +366,15 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
359366 def __contains__ (self , o : object ) -> bool : ...
360367 def __iter__ (self ) -> Iterator [_VT_co ]: ...
361368
362- @runtime
369+ @runtime_checkable
363370class ContextManager (Protocol [_T_co ]):
364371 def __enter__ (self ) -> _T_co : ...
365372 def __exit__ (self , __exc_type : Optional [Type [BaseException ]],
366373 __exc_value : Optional [BaseException ],
367374 __traceback : Optional [TracebackType ]) -> Optional [bool ]: ...
368375
369376if sys .version_info >= (3 , 5 ):
370- @runtime
377+ @runtime_checkable
371378 class AsyncContextManager (Protocol [_T_co ]):
372379 def __aenter__ (self ) -> Awaitable [_T_co ]: ...
373380 def __aexit__ (self , exc_type : Optional [Type [BaseException ]],
@@ -600,6 +607,17 @@ class NamedTuple(tuple):
600607 def _asdict (self ) -> collections .OrderedDict [str , Any ]: ...
601608 def _replace (self : _T , ** kwargs : Any ) -> _T : ...
602609
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+
603621def NewType (name : str , tp : Type [_T ]) -> Type [_T ]: ...
604622
605623# This itself is only available during type checking
0 commit comments