Add mypy fallback class for TypedDict methods to mypy_extensions (#2670)

This class is not defined at runtime but it's used by
mypy internally to support TypedDict methods.

Use NoReturn in argument types for better type safety
when the related mypy plugin hook is not active.
This commit is contained in:
Jukka Lehtosalo
2018-12-04 18:21:58 +00:00
committed by GitHub
parent 53d8756214
commit c8890b0f93

View File

@@ -1,8 +1,28 @@
from typing import Dict, Type, TypeVar, Optional, Union, Any, Generic
import abc
import sys
from typing import (
Dict, Type, TypeVar, Optional, Union, Any, Generic, Mapping, ItemsView, KeysView, ValuesView
)
_T = TypeVar('_T')
_U = TypeVar('_U')
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
def copy(self: _T) -> _T: ...
# Using NoReturn so that only calls using mypy plugin hook that specialize the signature
# can go through.
def setdefault(self, k: NoReturn, default: object) -> object: ...
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
def pop(self, k: NoReturn, default: _T = ...) -> object: ...
def update(self: _T, __m: _T) -> None: ...
if sys.version_info < (3, 0):
def has_key(self) -> bool: ...
def viewitems(self) -> ItemsView[str, object]: ...
def viewkeys(self) -> KeysView[str]: ...
def viewvalues(self) -> ValuesView[object]: ...
def __delitem__(self, k: NoReturn) -> None: ...
def TypedDict(typename: str, fields: Dict[str, Type[_T]], total: bool = ...) -> Type[dict]: ...
def Arg(type: _T = ..., name: Optional[str] = ...) -> _T: ...