Skip to content

Commit

Permalink
Changed typing_extensions.pyi to declare its own private version of `…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
erictraut authored Feb 5, 2022
1 parent 1371a1e commit 7dc5bed
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions stdlib/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 7dc5bed

Please sign in to comment.