From 7dc5bed72ced7e29dd556b1bf01d3439d2586391 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Fri, 4 Feb 2022 21:02:58 -0800 Subject: [PATCH] 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. --- stdlib/typing_extensions.pyi | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index f0b395119..a57bb629d 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -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