PEP 646: Make some final final readability improvements (#1862)

This commit is contained in:
Matthew Rahtz 2021-03-08 15:16:27 +00:00 committed by GitHub
parent 8ae39039a6
commit 4506987f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 12 deletions

View File

@ -116,17 +116,19 @@ We could also add annotations describing the actual size of each axis:
::
from typing import Literal
from typing import Literal as L
L640 = Literal[640]
L480 = Literal[480]
x: Array[int, L480, L640] = Array()
x: Array[float, L[480], L[640]] = Array()
For consistency, we use semantic axis annotations as the basis of the examples
in this PEP, but this PEP is agnostic about which of these two (or possibly other)
ways of using ``Array`` is preferable; that decision is left to library authors.
(Note also that for the rest of this PEP, for conciseness of example, we use
a simpler version of ``Array`` which is generic only in the shape - *not* the
data type.)
Specification
=============
@ -495,10 +497,10 @@ a similar way to regular type variables:
::
IntTuple = Tuple[int, *Ts]
NamedTuple = Tuple[str, Tuple[*Ts]]
NamedArray = Tuple[str, Array[*Ts]]
IntTuple[float, bool] # Equivalent to Tuple[int, float, bool]
NamedTuple[int, int] # Equivalent to Tuple[str, Tuple[int, int]]
NamedArray[Height] # Equivalent to Tuple[str, Array[Height]]
As this example shows, all type parameters passed to the alias are
bound to the type variable tuple.
@ -525,7 +527,7 @@ tuple in the alias is set empty:
::
IntTuple[()] # Equivalent to Tuple[int]
NamedTuple[()] # Equivalent to Tuple[str, Tuple[()]]
NamedArray[()] # Equivalent to Tuple[str, Array[()]]
If the type parameter list is omitted entirely, the alias is
compatible with arbitrary type parameters:
@ -547,12 +549,12 @@ Normal ``TypeVar`` instances can also be used in such aliases:
::
T = TypeVar('T')
Foo = Tuple[*Ts, T]
Foo = Tuple[T, *Ts]
# Ts bound to Tuple[int], T to int
# T bound to str, Ts to Tuple[int]
Foo[str, int]
# Ts bound to Tuple[()], T to int
Foo[int]
# T bound to float, Ts to Tuple[()]
Foo[float]
# T bound to Any, Ts to an arbitrary number of Any
Foo
@ -681,6 +683,12 @@ these type annotations.
Backwards Compatibility
=======================
The grammar changes necessary for unpacking of type variable tuples using
the star operator are almost all covered by PEP 637. The one exception
is '``*args: *Ts``', which would require an additional change. (This
change could be avoided by simply mandating this be written
as ``Unpack[Ts]``, however.)
The ``Unpack`` version of the PEP should be back-portable to previous
versions of Python.
@ -696,7 +704,7 @@ Reference Implementation
========================
Two reference implementations of type-checking functionality exist:
one in Pyre, as of TODO, and one in Pyright, as of v1.1.108.
one in Pyre, as of v0.9.0, and one in Pyright, as of v1.1.108.
A preliminary implementation of the ``Unpack`` version of the PEP in CPython
is available in `cpython/23527`_. A preliminary version of the version