Skip to content

Commit 01c2fa5

Browse files
authored
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`.
1 parent fcb97fe commit 01c2fa5

File tree

3 files changed

+74
-33
lines changed

3 files changed

+74
-33
lines changed

stdlib/2/typing.pyi

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ Protocol: _SpecialForm = ...
2222
Callable: _SpecialForm = ...
2323
Type: _SpecialForm = ...
2424
ClassVar: _SpecialForm = ...
25+
Final: _SpecialForm = ...
26+
_F = TypeVar('_F', bound=Callable[..., Any])
27+
def final(f: _F) -> _F: ...
28+
Literal: _SpecialForm = ...
29+
# TypedDict is a (non-subscriptable) special form.
30+
TypedDict: object = ...
2531

2632
class GenericMeta(type): ...
2733

@@ -65,52 +71,52 @@ _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
6571
_TC = TypeVar('_TC', bound=Type[object])
6672
_C = TypeVar("_C", bound=Callable)
6773

68-
def runtime(cls: _TC) -> _TC: ...
74+
def runtime_checkable(cls: _TC) -> _TC: ...
6975

70-
@runtime
76+
@runtime_checkable
7177
class SupportsInt(Protocol, metaclass=ABCMeta):
7278
@abstractmethod
7379
def __int__(self) -> int: ...
7480

75-
@runtime
81+
@runtime_checkable
7682
class SupportsFloat(Protocol, metaclass=ABCMeta):
7783
@abstractmethod
7884
def __float__(self) -> float: ...
7985

80-
@runtime
86+
@runtime_checkable
8187
class SupportsComplex(Protocol, metaclass=ABCMeta):
8288
@abstractmethod
8389
def __complex__(self) -> complex: ...
8490

85-
@runtime
91+
@runtime_checkable
8692
class SupportsAbs(Protocol[_T_co]):
8793
@abstractmethod
8894
def __abs__(self) -> _T_co: ...
8995

90-
@runtime
96+
@runtime_checkable
9197
class Reversible(Protocol[_T_co]):
9298
@abstractmethod
9399
def __reversed__(self) -> Iterator[_T_co]: ...
94100

95-
@runtime
101+
@runtime_checkable
96102
class Sized(Protocol, metaclass=ABCMeta):
97103
@abstractmethod
98104
def __len__(self) -> int: ...
99105

100-
@runtime
106+
@runtime_checkable
101107
class Hashable(Protocol, metaclass=ABCMeta):
102108
# TODO: This is special, in that a subclass of a hashable class may not be hashable
103109
# (for example, list vs. object). It's not obvious how to represent this. This class
104110
# is currently mostly useless for static checking.
105111
@abstractmethod
106112
def __hash__(self) -> int: ...
107113

108-
@runtime
114+
@runtime_checkable
109115
class Iterable(Protocol[_T_co]):
110116
@abstractmethod
111117
def __iter__(self) -> Iterator[_T_co]: ...
112118

113-
@runtime
119+
@runtime_checkable
114120
class Iterator(Iterable[_T_co], Protocol[_T_co]):
115121
@abstractmethod
116122
def next(self) -> _T_co: ...
@@ -135,7 +141,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
135141
@property
136142
def gi_running(self) -> bool: ...
137143

138-
@runtime
144+
@runtime_checkable
139145
class Container(Protocol[_T_co]):
140146
@abstractmethod
141147
def __contains__(self, x: object) -> bool: ...
@@ -234,7 +240,7 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
234240
def __contains__(self, o: object) -> bool: ...
235241
def __iter__(self) -> Iterator[_VT_co]: ...
236242

237-
@runtime
243+
@runtime_checkable
238244
class ContextManager(Protocol[_T_co]):
239245
def __enter__(self) -> _T_co: ...
240246
def __exit__(self, __exc_type: Optional[Type[BaseException]],
@@ -462,6 +468,21 @@ class NamedTuple(tuple):
462468
def _asdict(self) -> dict: ...
463469
def _replace(self: _T, **kwargs: Any) -> _T: ...
464470

471+
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
472+
class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
473+
def copy(self: _T) -> _T: ...
474+
# Using NoReturn so that only calls using mypy plugin hook that specialize the signature
475+
# can go through.
476+
def setdefault(self, k: NoReturn, default: object) -> object: ...
477+
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
478+
def pop(self, k: NoReturn, default: _T = ...) -> object: ...
479+
def update(self: _T, __m: _T) -> None: ...
480+
def has_key(self, k: str) -> bool: ...
481+
def viewitems(self) -> ItemsView[str, object]: ...
482+
def viewkeys(self) -> KeysView[str]: ...
483+
def viewvalues(self) -> ValuesView[object]: ...
484+
def __delitem__(self, k: NoReturn) -> None: ...
485+
465486
def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
466487

467488
# This itself is only available during type checking

stdlib/3/typing.pyi

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Protocol: _SpecialForm = ...
2323
Callable: _SpecialForm = ...
2424
Type: _SpecialForm = ...
2525
ClassVar: _SpecialForm = ...
26+
if sys.version_info >= (3, 8):
27+
Final: _SpecialForm = ...
28+
_F = TypeVar('_F', bound=Callable[..., Any])
29+
def final(f: _F) -> _F: ...
30+
Literal: _SpecialForm = ...
31+
# TypedDict is a (non-subscriptable) special form.
32+
TypedDict: object = ...
2633

2734
class GenericMeta(type): ...
2835

@@ -67,34 +74,34 @@ _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant.
6774
_TC = TypeVar('_TC', bound=Type[object])
6875
_C = TypeVar("_C", bound=Callable)
6976

70-
def runtime(cls: _TC) -> _TC: ...
77+
def runtime_checkable(cls: _TC) -> _TC: ...
7178

72-
@runtime
79+
@runtime_checkable
7380
class SupportsInt(Protocol, metaclass=ABCMeta):
7481
@abstractmethod
7582
def __int__(self) -> int: ...
7683

77-
@runtime
84+
@runtime_checkable
7885
class SupportsFloat(Protocol, metaclass=ABCMeta):
7986
@abstractmethod
8087
def __float__(self) -> float: ...
8188

82-
@runtime
89+
@runtime_checkable
8390
class SupportsComplex(Protocol, metaclass=ABCMeta):
8491
@abstractmethod
8592
def __complex__(self) -> complex: ...
8693

87-
@runtime
94+
@runtime_checkable
8895
class SupportsBytes(Protocol, metaclass=ABCMeta):
8996
@abstractmethod
9097
def __bytes__(self) -> bytes: ...
9198

92-
@runtime
99+
@runtime_checkable
93100
class SupportsAbs(Protocol[_T_co]):
94101
@abstractmethod
95102
def __abs__(self) -> _T_co: ...
96103

97-
@runtime
104+
@runtime_checkable
98105
class SupportsRound(Protocol[_T_co]):
99106
@overload
100107
@abstractmethod
@@ -103,30 +110,30 @@ class SupportsRound(Protocol[_T_co]):
103110
@abstractmethod
104111
def __round__(self, ndigits: int) -> _T_co: ...
105112

106-
@runtime
113+
@runtime_checkable
107114
class Reversible(Protocol[_T_co]):
108115
@abstractmethod
109116
def __reversed__(self) -> Iterator[_T_co]: ...
110117

111-
@runtime
118+
@runtime_checkable
112119
class Sized(Protocol, metaclass=ABCMeta):
113120
@abstractmethod
114121
def __len__(self) -> int: ...
115122

116-
@runtime
123+
@runtime_checkable
117124
class Hashable(Protocol, metaclass=ABCMeta):
118125
# TODO: This is special, in that a subclass of a hashable class may not be hashable
119126
# (for example, list vs. object). It's not obvious how to represent this. This class
120127
# is currently mostly useless for static checking.
121128
@abstractmethod
122129
def __hash__(self) -> int: ...
123130

124-
@runtime
131+
@runtime_checkable
125132
class Iterable(Protocol[_T_co]):
126133
@abstractmethod
127134
def __iter__(self) -> Iterator[_T_co]: ...
128135

129-
@runtime
136+
@runtime_checkable
130137
class Iterator(Iterable[_T_co], Protocol[_T_co]):
131138
@abstractmethod
132139
def __next__(self) -> _T_co: ...
@@ -162,7 +169,7 @@ class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):
162169
# Awaitable, AsyncIterator, AsyncIterable, Coroutine, Collection.
163170
# See https: //github.com/python/typeshed/issues/655 for why this is not easy.
164171

165-
@runtime
172+
@runtime_checkable
166173
class Awaitable(Protocol[_T_co]):
167174
@abstractmethod
168175
def __await__(self) -> Generator[Any, None, _T_co]: ...
@@ -193,12 +200,12 @@ class Coroutine(Awaitable[_V_co], Generic[_T_co, _T_contra, _V_co]):
193200
class AwaitableGenerator(Awaitable[_V_co], Generator[_T_co, _T_contra, _V_co],
194201
Generic[_T_co, _T_contra, _V_co, _S], metaclass=ABCMeta): ...
195202

196-
@runtime
203+
@runtime_checkable
197204
class AsyncIterable(Protocol[_T_co]):
198205
@abstractmethod
199206
def __aiter__(self) -> AsyncIterator[_T_co]: ...
200207

201-
@runtime
208+
@runtime_checkable
202209
class AsyncIterator(AsyncIterable[_T_co],
203210
Protocol[_T_co]):
204211
@abstractmethod
@@ -232,22 +239,22 @@ if sys.version_info >= (3, 6):
232239
@property
233240
def ag_running(self) -> bool: ...
234241

235-
@runtime
242+
@runtime_checkable
236243
class Container(Protocol[_T_co]):
237244
@abstractmethod
238245
def __contains__(self, __x: object) -> bool: ...
239246

240247

241248
if sys.version_info >= (3, 6):
242-
@runtime
249+
@runtime_checkable
243250
class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
244251
# Implement Sized (but don't have it as a base class).
245252
@abstractmethod
246253
def __len__(self) -> int: ...
247254

248255
_Collection = Collection
249256
else:
250-
@runtime
257+
@runtime_checkable
251258
class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
252259
# Implement Sized (but don't have it as a base class).
253260
@abstractmethod
@@ -359,15 +366,15 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
359366
def __contains__(self, o: object) -> bool: ...
360367
def __iter__(self) -> Iterator[_VT_co]: ...
361368

362-
@runtime
369+
@runtime_checkable
363370
class ContextManager(Protocol[_T_co]):
364371
def __enter__(self) -> _T_co: ...
365372
def __exit__(self, __exc_type: Optional[Type[BaseException]],
366373
__exc_value: Optional[BaseException],
367374
__traceback: Optional[TracebackType]) -> Optional[bool]: ...
368375

369376
if sys.version_info >= (3, 5):
370-
@runtime
377+
@runtime_checkable
371378
class AsyncContextManager(Protocol[_T_co]):
372379
def __aenter__(self) -> Awaitable[_T_co]: ...
373380
def __aexit__(self, exc_type: Optional[Type[BaseException]],
@@ -600,6 +607,17 @@ class NamedTuple(tuple):
600607
def _asdict(self) -> collections.OrderedDict[str, Any]: ...
601608
def _replace(self: _T, **kwargs: Any) -> _T: ...
602609

610+
# Internal mypy fallback type for all typed dicts (does not exist at runtime)
611+
class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
612+
def copy(self: _T) -> _T: ...
613+
# Using NoReturn so that only calls using mypy plugin hook that specialize the signature
614+
# can go through.
615+
def setdefault(self, k: NoReturn, default: object) -> object: ...
616+
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
617+
def pop(self, k: NoReturn, default: _T = ...) -> object: ...
618+
def update(self: _T, __m: _T) -> None: ...
619+
def __delitem__(self, k: NoReturn) -> None: ...
620+
603621
def NewType(name: str, tp: Type[_T]) -> Type[_T]: ...
604622

605623
# This itself is only available during type checking

third_party/2and3/typing_extensions.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ _F = TypeVar('_F', bound=Callable[..., Any])
1919
_TC = TypeVar('_TC', bound=Type[object])
2020
class _SpecialForm:
2121
def __getitem__(self, typeargs: Any) -> Any: ...
22-
def runtime(cls: _TC) -> _TC: ...
22+
def runtime_checkable(cls: _TC) -> _TC: ...
23+
# This alias for above is kept here for backwards compatibility.
24+
runtime = runtime_checkable
2325
Protocol: _SpecialForm = ...
2426
Final: _SpecialForm = ...
2527
def final(f: _F) -> _F: ...

0 commit comments

Comments
 (0)