PEP 646: Update based on typing-sig discussions #1781
Conversation
Feature removals: * Remove `Map` (to be added in a separate PEP) * Remove support for all forms of concatenation other than simply prefixing of a single `TypeVarTuple` (fancier forms of concatenation to also be added in a separate PEP) Semantic changes: * Try switching `Ts` back to mean the 'unpacked' version (such that `Ts = int, str; Tuple[Ts] = Tuple[int, str]`) to see whether everything still works. (Might revert this, pending future discussion in typing-sig.)
I don't think this would be a problem. Yes, |
|
I am waiting for @mrahtz to make up his mind on whether he really wants to switch to |
Semantic changes: * Switch back to `Ts` meaning types packed in a `Tuple` * Disallow unpacked usages of `Ts` * Unparameterised `Tensor` behaves like `Tensor[Any, ...]` Clarifications: * `TypeVarTuple`s are invariant * `TypeVarTuple`s can not be bound to unknown-length types (e.g. `Tuple[int, ...]` * `TypeVarTuple`s can not be bound to unions of types * Empty `*args` behaves like `Tuple[()]` * `TypeVarTuple`s can not appear in slices Readability changes: * Reorder some sections for better flow
|
No one has vetoed |
|
You are correct that My initial implementation of PEP 646 in pyright includes support for variadic type variables that appear within the It would be good to clarify in PEP 646 that callback protocols with variadic type variables are supported. |
|
The latest draft is looking really good. |
| * A new use for the star operator: unpacking of each individual type | ||
| from a ``TypeVarTuple``. | ||
| * Two new type operators, ``Unpack`` and ``Map``. | ||
| In order to support the above use-cases, we introduce ``TypeVarTuple``. This serves as a placeholder not for a single type but for an *arbitrary* number of types, and behaving like a number of ``TypeVar`` instances packed in a ``Tuple``. |
erictraut
Jan 31, 2021
Contributor
Nit: "use cases" should be two words, not hyphenated.
Nit: "use cases" should be two words, not hyphenated.
| In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows | ||
| us to parameterise the class with an *arbitrary* number of type parameters. | ||
| That is, in addition to being able to define rank-2 arrays such as | ||
| ``Array[Height, Width]``, we could also defined rank-3 arrays, rank-4 arrays, |
erictraut
Jan 31, 2021
Contributor
Minor typo: "defined" should be "define.
Minor typo: "defined" should be "define.
| # NOT valid | ||
| def foo(**kwargs: Ts): ... | ||
| def foo(**kwargs: *Ts): ... | ||
| * To avoid potential confusion about whether to use a type variable tuple |
erictraut
Jan 31, 2021
Contributor
I really like this new section. It provides a strong and clear justification for always unpacking and for including the unpack operator.
I really like this new section. It provides a strong and clear justification for always unpacking and for including the unpack operator.
| Type variable tuples can also be used in the arguments section of a | ||
| ``Callable``: | ||
| Note that the use of the star operator in this context requires a grammar change, | ||
| and is therefore only available in new versions of Python. To enable use of type |
erictraut
Jan 31, 2021
Contributor
Grammar nit: "only available" should be "available only".
Grammar nit: "only available" should be "available only".
|
|
||
| ``Map`` | ||
| ------- | ||
| Behaviour when Type Parameters are not Specified |
erictraut
Jan 31, 2021
Contributor
Nit: "Behaviour" -> "Behavior", unless you are intentionally using the British spelling. If so, then ignore.
Nit: "Behaviour" -> "Behavior", unless you are intentionally using the British spelling. If so, then ignore.
| @@ -590,7 +442,7 @@ a similar way to regular type variables: | |||
|
|
|||
| :: | |||
|
|
|||
| IntTuple = Tuple[int, *Ts] | |||
| IntTuple = Tuple[int, Ts] | |||
erictraut
Jan 31, 2021
Contributor
Ts -> *Ts — it needs to be unpacked
Ts -> *Ts — it needs to be unpacked
| @@ -601,51 +453,47 @@ the type variable tuple holds no types: | |||
|
|
|||
| IntTuple # Equivalent to Tuple[int] | |||
erictraut
Jan 31, 2021
Contributor
I recommend also giving an example of IntTuple[()], which is equivalent to Tuple[int] but is more explicit.
I recommend also giving an example of IntTuple[()], which is equivalent to Tuple[int] but is more explicit.
| @@ -677,7 +525,7 @@ in a complete array type. | |||
| # E.g. Shape[Height, Width] | |||
| # (Where Height and Width are custom types) | |||
| Axes = TypeVarTuple('Axes') | |||
| class Shape(Generic[*Axes]): ... | |||
| class Shape(Generic[Axes]): ... | |||
erictraut
Jan 31, 2021
Contributor
Axes needs to be unpacked here, so the * should not be removed.
Axes needs to be unpacked here, so the * should not be removed.
| @@ -689,10 +537,15 @@ in a complete array type. | |||
|
|
|||
| # Type aliases for less verbosity | |||
| # E.g. Float32Array[Height, Width, Channels] | |||
| Float32Array = Array[np.float32, Shape[*Axes]] | |||
| Float32Array = Array[np.float32, Shape[Axes]] | |||
erictraut
Jan 31, 2021
Contributor
Axes needs to be unpacked here, so the * should not be removed.
Axes needs to be unpacked here, so the * should not be removed.
| However, note that as of this PEP, if a type variable tuple does appear in | ||
| the arguments section of a ``Callable``, it must appear alone. | ||
| That is, `Type Prefixing` is not supported in the context of ``Callable``. | ||
| (PEP 612 covers the use-cases where this might otherwise be useful.) |
erictraut
Jan 31, 2021
Contributor
I recommend that you also include a sentence about compatibility with callback prototypes as defined in PEP 544. In particular, TypeVarTuple can be used in the __call__ signature of a callback prototype.
I recommend that you also include a sentence about compatibility with callback prototypes as defined in PEP 544. In particular, TypeVarTuple can be used in the __call__ signature of a callback prototype.


Feature removals:
Map(to be added in a separate PEP)TypeVarTuple(fancier forms of concatenation to also be added in a separate PEP)Semantic changes:
Tsback to mean the 'unpacked' version (such thatTs = int, str; Tuple[Ts] = Tuple[int, str]) to see whether everything still works. (Might revert this, pending future discussion in typing-sig.)Clarifications:
Callable. Why?Callable[[int, Ts], Any], then the first argument would have to be positional-only, and that seems like it'll have complications.