Skip to content

Commit bacf74a

Browse files
authored
typing: make Protocol definition reflect runtime (#16386)
Co-authored-by: istoolsfox <[email protected]>
1 parent 44fe292 commit bacf74a

5 files changed

Lines changed: 9 additions & 5 deletions

File tree

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ typing.type_check_only # typing decorator that is not available at runtime
396396
# Details of runtime definition don't need to be in stubs
397397
typing._Final
398398
typing._Final.__init_subclass__
399-
typing\.Protocol
400399
typing(_extensions)?\._TypedDict
401400
typing(_extensions)?\.Any.*
402401
typing(_extensions)?\.TypedDict

stdlib/@tests/stubtest_allowlists/py314.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ multiprocessing.managers._BaseDictProxy.values
156156
# To match `dict`, we lie about the runtime, but use overloads to match the correct behavior
157157
types.MappingProxyType.get
158158

159-
typing_extensions.Protocol # Super-special typing primitive
160159

161160

162161
# =============================================================

stdlib/@tests/stubtest_allowlists/py315.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ multiprocessing.managers._BaseDictProxy.values
129129
# To match `dict`, we lie about the runtime, but use overloads to match the correct behavior.
130130
types.MappingProxyType.get
131131

132-
typing_extensions.Protocol # Super-special typing primitive
133132

134133
copy.replace # Cannot have keyword-only ParamSpec
135134

stdlib/typing.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ class _SpecialForm(_Final):
238238
def __ror__(self, other: Any) -> _SpecialForm: ...
239239

240240
Union: _SpecialForm
241-
Protocol: _SpecialForm
242241
Callable: _SpecialForm
243242
Type: _SpecialForm
244243
NoReturn: _SpecialForm
@@ -472,6 +471,11 @@ class _Generic:
472471

473472
Generic: type[_Generic]
474473

474+
@type_check_only
475+
class _Protocol: ...
476+
477+
Protocol: type[_Protocol]
478+
475479
class _ProtocolMeta(ABCMeta):
476480
if sys.version_info >= (3, 12):
477481
def __init__(cls, *args: Any, **kwargs: Any) -> None: ...

stdlib/typing_extensions.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,15 @@ _T_contra = _TypeVar("_T_contra", contravariant=True)
215215
if sys.version_info < (3, 15):
216216
def no_type_check_decorator(decorator: _F) -> _F: ...
217217

218+
@type_check_only
219+
class _Protocol: ...
220+
218221
# Do not import (and re-export) Protocol or runtime_checkable from
219222
# typing module because type checkers need to be able to distinguish
220223
# typing.Protocol and typing_extensions.Protocol so they can properly
221224
# warn users about potential runtime exceptions when using typing.Protocol
222225
# on older versions of Python.
223-
Protocol: _SpecialForm
226+
Protocol: type[_Protocol]
224227

225228
def runtime_checkable(cls: _TC) -> _TC: ...
226229

0 commit comments

Comments
 (0)