From d61635e2431cd8f5aada6629a3e53505794047b4 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 7 May 2019 14:45:45 -0400 Subject: [PATCH] Add TypedDict to typing_extensions (#2940) Since there is a PEP for `TypedDict` (PEP 589), it is being added to `typing_extension`, see https://github.com/python/typing/pull/628. This PR essentially copies the definition from `mypy_extensions`. Corresponding mypy PR https://github.com/python/mypy/pull/6744 can be merged independently of this PR. --- third_party/2and3/typing_extensions.pyi | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/third_party/2and3/typing_extensions.pyi b/third_party/2and3/typing_extensions.pyi index 8ac31fc35..fdc9d874d 100644 --- a/third_party/2and3/typing_extensions.pyi +++ b/third_party/2and3/typing_extensions.pyi @@ -1,3 +1,4 @@ +import abc import sys from typing import Callable from typing import ClassVar as ClassVar @@ -11,8 +12,9 @@ from typing import overload as overload from typing import Text as Text from typing import Type as Type from typing import TYPE_CHECKING as TYPE_CHECKING -from typing import TypeVar, Any +from typing import TypeVar, Any, Mapping, ItemsView, KeysView, ValuesView, Dict, Type +_T = TypeVar('_T') _F = TypeVar('_F', bound=Callable[..., Any]) _TC = TypeVar('_TC', bound=Type[object]) class _SpecialForm: @@ -23,6 +25,25 @@ Final: _SpecialForm = ... def final(f: _F) -> _F: ... Literal: _SpecialForm = ... +# 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, k: str) -> bool: ... + def viewitems(self) -> ItemsView[str, object]: ... + def viewkeys(self) -> KeysView[str]: ... + def viewvalues(self) -> ValuesView[object]: ... + def __delitem__(self, k: NoReturn) -> None: ... + +# TypedDict is a (non-subscriptable) special form. +TypedDict: object = ... + if sys.version_info >= (3, 3): from typing import ChainMap as ChainMap