Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 646: Make some final final readability improvements #1862

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions pep-0646.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============

Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down