Changed typing_extensions.pyi to declare its own private version of Protocol (#7133)

Changed typing_extensions.pyi to declare its own private version of `Protocol` and `runtime_checkable` rather than re-exporting the symbols imported from `typing`. This allows pyright to warn users about runtime exceptions when they attempt to use typing.Protocol on versions of Python prior to 3.7.
This commit is contained in:
Eric Traut
2022-02-04 21:02:58 -08:00
committed by GitHub
parent 1371a1e1fe
commit 7dc5bed72c

View File

@@ -22,18 +22,17 @@ from typing import ( # noqa Y022
Mapping,
NewType as NewType,
NoReturn as NoReturn,
Protocol as Protocol,
Text as Text,
Type as Type,
TypeVar,
ValuesView,
_Alias,
overload as overload,
runtime_checkable as runtime_checkable,
)
_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., Any])
_TC = TypeVar("_TC", bound=Type[object])
# unfortunately we have to duplicate this class definition from typing.pyi or we break pytype
class _SpecialForm:
@@ -42,6 +41,15 @@ class _SpecialForm:
def __or__(self, other: Any) -> _SpecialForm: ...
def __ror__(self, other: Any) -> _SpecialForm: ...
# Do not import (and re-export) Protocol or runtime_checkable from
# typing module because type checkers need to be able to distinguish
# typing.Protocol and typing_extensions.Protocol so they can properly
# warn users about potential runtime exceptions when using typing.Protocol
# on older versions of Python.
Protocol: _SpecialForm = ...
def runtime_checkable(cls: _TC) -> _TC: ...
# This alias for above is kept here for backwards compatibility.
runtime = runtime_checkable
Final: _SpecialForm