Visitar URL original
PEP 646: Update based on typing-sig discussions by mrahtz · Pull Request #1781 · python/peps · GitHub
The Wayback Machine - https://web.archive.org/web/20210202062141/https://github.com/python/peps/pull/1781
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: Update based on typing-sig discussions #1781

Open
wants to merge 3 commits into
base: master
from

Conversation

@mrahtz
Copy link
Contributor

@mrahtz mrahtz commented Jan 25, 2021 •

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.)

Clarifications:

  • Type prefixing is not supported in Callable. Why?
    • Stay in the spirit of 'keep this PEP simple, gosh darn it'.
    • If we had Callable[[int, Ts], Any], then the first argument would have to be positional-only, and that seems like it'll have complications.
    • I expect most use-cases to be covered by PEP 612.
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.)
@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 25, 2021 •

  • If we had Callable[[int, Ts], Any], then the first argument would have to be positional-only, and that seems like it'll have complications.

I don't think this would be a problem. Yes, Callable can only describe positional signatures. But no, this does not require that functions need to be given positional-only parameters in order to conform to a Callable type. The int in your example is not different from the int in Callable[[int, str], str].

@brettcannon brettcannon requested a review from gvanrossum Jan 26, 2021
@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 27, 2021

I am waiting for @mrahtz to make up his mind on whether he really wants to switch to Tuple[Ts] instead of Tuple[*Ts].

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
@mrahtz mrahtz changed the title PEP 646: Simplify based on typing-sig discussions PEP 646: Update based on typing-sig discussions Jan 31, 2021
@mrahtz
Copy link
Contributor Author

@mrahtz mrahtz commented Jan 31, 2021

No one has vetoed Tuple[*Ts] on typing-sig, and I feel good with it too, so I'm happy for this to be merged.

@erictraut
Copy link
Contributor

@erictraut erictraut commented Jan 31, 2021

You are correct that Callable supports positional arguments only, but callback protocols (as defined in PEP 544) do not have this limitation.

My initial implementation of PEP 646 in pyright includes support for variadic type variables that appear within the __call__ signature of a callback protocol. Here is a simple test case that demonstrates this.

It would be good to clarify in PEP 646 that callback protocols with variadic type variables are supported.

Copy link
Contributor

@erictraut erictraut left a comment

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``.

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

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,

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

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

This comment has been minimized.

@erictraut

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.

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

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

Grammar nit: "only available" should be "available only".


``Map``
-------
Behaviour when Type Parameters are not Specified

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

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]

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

Ts -> *Ts — it needs to be unpacked

@@ -601,51 +453,47 @@ the type variable tuple holds no types:

IntTuple # Equivalent to Tuple[int]

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

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]): ...

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

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]]

This comment has been minimized.

@erictraut

erictraut Jan 31, 2021
Contributor

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.)

This comment has been minimized.

@erictraut

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants