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] Explicitly allow *args: *Tuple[*Ts, T] #2125

Closed
wants to merge 7 commits into from
17 changes: 15 additions & 2 deletions pep-0646.rst
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ must be of the type annotated. That is, if we specify ``*args`` to be type ``int
then *all* arguments must be of type ``int``. This limits our ability to specify
the type signatures of functions that take heterogeneous argument types.

If ``*args`` is annotated as a type variable tuple, however, the types of the
individual arguments become the types in the type variable tuple:
If ``*args`` is annotated as a star-prefixed type variable tuple, however, the
types of the individual arguments become the types in the type variable tuple:

::

Expand All @@ -436,6 +436,19 @@ individual arguments become the types in the type variable tuple:
If no arguments are passed, the type variable tuple behaves like an
empty tuple, ``Tuple[()]``.

It is also possible to annotate ``*args`` as a star-prefixed tuple parameterised
using a type variable tuple. This enables referral to suffixes of the variadic
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
argument list. For example:

::

# os.execle takes arguments 'path, arg0, arg1, ..., env'
def execle(path: str, *args: *Tuple[*Ts, Mapping]) → None: ...
mrahtz marked this conversation as resolved.
Show resolved Hide resolved

# Note that this is different to
def execle(path: str, *args: *Ts, env: Mapping) → None: ...
# which would make `env` a keyword-only argument.

Note that, in keeping with the rule that type variable tuples must always
be used unpacked, annotating ``*args`` as being a plain type variable tuple
instance is *not* allowed:
Expand Down