Sync recent typing and typing_extensions updates (#3070)

This includes two things to sync up with recent runtime updates:
* Move `Final`, `@final`, `Literal`, and `TypedDict` to `typing` (`typing_extensions` still defines or re-exports them)
* Rename `@typing.runtime` to `@typing.runtime_checkable`, while keeping `@runtime` as a backwards-compatible alias in `typing_extensions`.
This commit is contained in:
Ivan Levkivskyi
2019-06-18 02:31:54 +01:00
committed by GitHub
parent fcb97fe8f1
commit 01c2fa5a14
3 changed files with 74 additions and 33 deletions

View File

@@ -22,6 +22,12 @@ Protocol: _SpecialForm = ...
Callable: _SpecialForm = ...
Type: _SpecialForm = ...
ClassVar: _SpecialForm = ...
Final: _SpecialForm = ...
_F = TypeVar('_F', bound=Callable[..., Any])
def final(f: _F) -> _F: ...
Literal: _SpecialForm = ...
# TypedDict is a (non-subscriptable) special form.
TypedDict: object = ...
class GenericMeta(type): ...
@@ -65,39 +71,39 @@ _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
_TC = TypeVar('_TC', bound=Type[object])
_C = TypeVar("_C", bound=Callable)
def runtime(cls: _TC) -> _TC: ...
def runtime_checkable(cls: _TC) -> _TC: ...
@runtime
@runtime_checkable
class SupportsInt(Protocol, metaclass=ABCMeta):
@abstractmethod
def __int__(self) -> int: ...
@runtime
@runtime_checkable
class SupportsFloat(Protocol, metaclass=ABCMeta):
@abstractmethod
def __float__(self) -> float: ...
@runtime
@runtime_checkable
class SupportsComplex(Protocol, metaclass=ABCMeta):
@abstractmethod
def __complex__(self) -> complex: ...
@runtime
@runtime_checkable
class SupportsAbs(Protocol[_T_co]):
@abstractmethod
def __abs__(self) -> _T_co: ...
@runtime
@runtime_checkable
class Reversible(Protocol[_T_co]):
@abstractmethod
def __reversed__(self) -> Iterator[_T_co]: ...
@runtime
@runtime_checkable
class Sized(Protocol, metaclass=ABCMeta):
@abstractmethod
def __len__(self) -> int: ...
@runtime
@runtime_checkable
class Hashable(Protocol, metaclass=ABCMeta):
# TODO: This is special, in that a subclass of a hashable class may not be hashable
# (for example, list vs. object). It's not obvious how to represent this. This class
@@ -105,12 +111,12 @@ class Hashable(Protocol, metaclass=ABCMeta):
@abstractmethod
def __hash__(self) -> int: ...
@runtime
@runtime_checkable
class Iterable(Protocol[_T_co]):
@abstractmethod
def __iter__(self) -> Iterator[_T_co]: ...
@runtime
@runtime_checkable
class Iterator(Iterable[_T_co], Protocol[_T_co]):
@abstractmethod
def next(self) -> _T_co: ...
@@ -135,7 +141,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
@property
def gi_running(self) -> bool: ...
@runtime
@runtime_checkable
class Container(Protocol[_T_co]):
@abstractmethod
def __contains__(self, x: object) -> bool: ...
@@ -234,7 +240,7 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...
@runtime
@runtime_checkable
class ContextManager(Protocol[_T_co]):
def __enter__(self) -> _T_co: ...
def __exit__(self, __exc_type: Optional[Type[BaseException]],
@@ -462,6 +468,21 @@ class NamedTuple(tuple):
def _asdict(self) -> dict: ...
def _replace(self: _T, **kwargs: Any) -> _T: ...
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
class _TypedDict(Mapping[str, object], metaclass=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: ...
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: ...
def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
# This itself is only available during type checking

View File

@@ -23,6 +23,13 @@ Protocol: _SpecialForm = ...
Callable: _SpecialForm = ...
Type: _SpecialForm = ...
ClassVar: _SpecialForm = ...
if sys.version_info >= (3, 8):
Final: _SpecialForm = ...
_F = TypeVar('_F', bound=Callable[..., Any])
def final(f: _F) -> _F: ...
Literal: _SpecialForm = ...
# TypedDict is a (non-subscriptable) special form.
TypedDict: object = ...
class GenericMeta(type): ...
@@ -67,34 +74,34 @@ _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
_TC = TypeVar('_TC', bound=Type[object])
_C = TypeVar("_C", bound=Callable)
def runtime(cls: _TC) -> _TC: ...
def runtime_checkable(cls: _TC) -> _TC: ...
@runtime
@runtime_checkable
class SupportsInt(Protocol, metaclass=ABCMeta):
@abstractmethod
def __int__(self) -> int: ...
@runtime
@runtime_checkable
class SupportsFloat(Protocol, metaclass=ABCMeta):
@abstractmethod
def __float__(self) -> float: ...
@runtime
@runtime_checkable
class SupportsComplex(Protocol, metaclass=ABCMeta):
@abstractmethod
def __complex__(self) -> complex: ...
@runtime
@runtime_checkable
class SupportsBytes(Protocol, metaclass=ABCMeta):
@abstractmethod
def __bytes__(self) -> bytes: ...
@runtime
@runtime_checkable
class SupportsAbs(Protocol[_T_co]):
@abstractmethod
def __abs__(self) -> _T_co: ...
@runtime
@runtime_checkable
class SupportsRound(Protocol[_T_co]):
@overload
@abstractmethod
@@ -103,17 +110,17 @@ class SupportsRound(Protocol[_T_co]):
@abstractmethod
def __round__(self, ndigits: int) -> _T_co: ...
@runtime
@runtime_checkable
class Reversible(Protocol[_T_co]):
@abstractmethod
def __reversed__(self) -> Iterator[_T_co]: ...
@runtime
@runtime_checkable
class Sized(Protocol, metaclass=ABCMeta):
@abstractmethod
def __len__(self) -> int: ...
@runtime
@runtime_checkable
class Hashable(Protocol, metaclass=ABCMeta):
# TODO: This is special, in that a subclass of a hashable class may not be hashable
# (for example, list vs. object). It's not obvious how to represent this. This class
@@ -121,12 +128,12 @@ class Hashable(Protocol, metaclass=ABCMeta):
@abstractmethod
def __hash__(self) -> int: ...
@runtime
@runtime_checkable
class Iterable(Protocol[_T_co]):
@abstractmethod
def __iter__(self) -> Iterator[_T_co]: ...
@runtime
@runtime_checkable
class Iterator(Iterable[_T_co], Protocol[_T_co]):
@abstractmethod
def __next__(self) -> _T_co: ...
@@ -162,7 +169,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
@runtime
@runtime_checkable
class Awaitable(Protocol[_T_co]):
@abstractmethod
def __await__(self) -> Generator[Any, None, _T_co]: ...
@@ -193,12 +200,12 @@ class Coroutine(Awaitable[_V_co], Generic[_T_co, _T_contra, _V_co]):
class AwaitableGenerator(Awaitable[_V_co], Generator[_T_co, _T_contra, _V_co],
Generic[_T_co, _T_contra, _V_co, _S], metaclass=ABCMeta): ...
@runtime
@runtime_checkable
class AsyncIterable(Protocol[_T_co]):
@abstractmethod
def __aiter__(self) -> AsyncIterator[_T_co]: ...
@runtime
@runtime_checkable
class AsyncIterator(AsyncIterable[_T_co],
Protocol[_T_co]):
@abstractmethod
@@ -232,14 +239,14 @@ if sys.version_info >= (3, 6):
@property
def ag_running(self) -> bool: ...
@runtime
@runtime_checkable
class Container(Protocol[_T_co]):
@abstractmethod
def __contains__(self, __x: object) -> bool: ...
if sys.version_info >= (3, 6):
@runtime
@runtime_checkable
class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
# Implement Sized (but don't have it as a base class).
@abstractmethod
@@ -247,7 +254,7 @@ if sys.version_info >= (3, 6):
_Collection = Collection
else:
@runtime
@runtime_checkable
class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
# Implement Sized (but don't have it as a base class).
@abstractmethod
@@ -359,7 +366,7 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...
@runtime
@runtime_checkable
class ContextManager(Protocol[_T_co]):
def __enter__(self) -> _T_co: ...
def __exit__(self, __exc_type: Optional[Type[BaseException]],
@@ -367,7 +374,7 @@ class ContextManager(Protocol[_T_co]):
__traceback: Optional[TracebackType]) -> Optional[bool]: ...
if sys.version_info >= (3, 5):
@runtime
@runtime_checkable
class AsyncContextManager(Protocol[_T_co]):
def __aenter__(self) -> Awaitable[_T_co]: ...
def __aexit__(self, exc_type: Optional[Type[BaseException]],
@@ -600,6 +607,17 @@ class NamedTuple(tuple):
def _asdict(self) -> collections.OrderedDict[str, Any]: ...
def _replace(self: _T, **kwargs: Any) -> _T: ...
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
class _TypedDict(Mapping[str, object], metaclass=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: ...
def __delitem__(self, k: NoReturn) -> None: ...
def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
# This itself is only available during type checking

View File

@@ -19,7 +19,9 @@ _F = TypeVar('_F', bound=Callable[..., Any])
_TC = TypeVar('_TC', bound=Type[object])
class _SpecialForm:
def __getitem__(self, typeargs: Any) -> Any: ...
def runtime(cls: _TC) -> _TC: ...
def runtime_checkable(cls: _TC) -> _TC: ...
# This alias for above is kept here for backwards compatibility.
runtime = runtime_checkable
Protocol: _SpecialForm = ...
Final: _SpecialForm = ...
def final(f: _F) -> _F: ...