You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: peps/pep-0718.rst
+12-32Lines changed: 12 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -201,28 +201,9 @@ The following code snippet would fail at runtime without this change as
201
201
Interactions with ``@typing.overload``
202
202
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203
203
204
-
Overloaded functions should work much the same as already, since they have no effect on
205
-
the runtime type. This change will lead to more expressiveness with user's able to
206
-
decide and the behaviour/overload can be specified by the developer rather than leaving
207
-
it to ordering of overloads/unions.
208
-
209
-
.. code-block:: python
210
-
211
-
# N.B. `class bytes(Sequence[int]): ...` and `Foo` is a non-specified generic type
212
-
@overload
213
-
def seq_first[T: Sequence[int]](x: T) -> T: ...
214
-
@overload
215
-
def seq_first[T: bytes](x: T) -> Foo[T]: ...
216
-
217
-
@overload
218
-
def bytes_first[T: bytes](x: T) -> Foo[T]: ...
219
-
@overload
220
-
def bytes_first[T: Sequence[int]](x: T) -> T: ...
221
-
222
-
reveal_type(seq_first(b"")) # type is bytes
223
-
reveal_type(bytes_first(b"")) # type is Foo[bytes]
224
-
225
-
Explicit specialisation will restrict the set of available overloads
204
+
Overloaded functions should work much the same as they already do, since they do not
205
+
affect the runtime type. Explicit specialisation will restrict the set of available
206
+
overloads.
226
207
227
208
.. code-block:: python
228
209
@@ -232,33 +213,32 @@ Explicit specialisation will restrict the set of available overloads
232
213
defmake(x: str, y: str) -> tuple[int, int]: ...
233
214
234
215
reveal_type(make[int](1)) # type is int
235
-
reveal_type(make[int]("foo", "bar")) # Invalid: no overload for `make[int](x:str, y: str)` found, a similar overload exists but explicit specialisation prevented its use
216
+
reveal_type(make[int]("foo", "bar")) # Invalid: no overload for `make[int](x:str, y: str)` found, a similar overload exists but explicit specialisation prevented its use
236
217
237
218
Functions Parameterized by ``TypeVarTuple``\ s
238
219
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
239
-
Currently type checkers disallow the use of multiple ``TypeVarTuple``\s in it's
240
-
generic parameters, however it is currently valid to have a function as such
220
+
Currently, type checkers disallow the use of multiple ``TypeVarTuple``\s in their
221
+
generic parameters; however, it is currently valid to have a function as such:
241
222
242
223
.. code-block:: python
243
224
244
225
def foo[*T, *U](bar: Bar[*T], baz: Baz[*U]): ...
245
226
def spam[*T](bar: Bar[*T]): ...
246
227
247
-
This PEP does not allow similar functions to be subscripted, for the same reason as
248
-
defined in :pep:`PEP 646<646#multiple-type-variable-tuples-not-allowed>`.
228
+
This PEP does not allow functions like ``foo`` to be subscripted, for the same reason
229
+
as defined in :pep:`PEP 646<646#multiple-type-variable-tuples-not-allowed>`.
249
230
250
231
.. code-block:: python
251
232
252
233
foo[int, str, bool, complex](Bar(), Baz()) # Invalid: cannot determine which parameters are passed to *T and *U. Explicitly parameterise the instances individually
253
234
spam[int, str, bool, complex](Bar()) # OK
254
235
255
-
256
236
Binding Rules
257
237
^^^^^^^^^^^^^
258
-
Subscriptions on methods (including classmethods, staticmethods etc.) should only have
259
-
access to their function's type parameters and not the enclosing class's. Subscription
260
-
should follow the rules specified in :pep:`PEP 696<696#binding-rules>` methods should
261
-
be bound on attribute access.
238
+
Method subscription (including ``classmethods``, ``staticmethods``, etc.) should only
239
+
have access tos their function's type parameter and not the enclosing class's.
240
+
Subscription should follow the rules specified in :pep:`PEP 696<696#binding-rules>`;
241
+
methods should bind type parameters on attribute access.
0 commit comments