Skip to content

Commit 6d5c08b

Browse files
committed
PEP 646: fix nits and typos.
1 parent 383a441 commit 6d5c08b

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

pep-0646.rst

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,6 @@ As of this PEP, only a single type variable tuple may appear in a type parameter
315315

316316
class Array(Generic[*Ts1, *Ts2]): ... # Error
317317

318-
Only one unpacking may appear in a tuple:
319-
320-
::
321-
322-
x: Tuple[int, *Ts, str, *Ts2] # Error
323-
y: Tuple[int, *Tuple[int, ...], str, *Tuple[str, ...]] # Error
324-
325318
Type Concatenation
326319
------------------
327320

@@ -368,6 +361,15 @@ We mentioned that a ``TypeVarTuple`` simply stands for a tuple of
368361
types. Thus, we can cleanly replace any use of a ``TypeVarTuple`` in a
369362
function signature with a tuple type.
370363

364+
365+
As with ``TypeVarTuple``, only one unpacking may appear in a tuple:
366+
367+
::
368+
369+
x: Tuple[int, *Ts, str, *Ts2] # Error
370+
y: Tuple[int, *Tuple[int, ...], str, *Tuple[str, ...]] # Error
371+
372+
371373
Unpacking a Concrete Tuple Type
372374
'''''''''''''''''''''''''''''''
373375

@@ -406,7 +408,7 @@ last element is guaranteed to be of type ``str``, and there may be
406408
zero or more elements of type ``str`` in between. Note that
407409
``Tuple[*Tuple[int, ...]]`` is equivalent to ``Tuple[int, ...]``.
408410

409-
Consider the following functions:
411+
Consider the following function:
410412

411413
::
412414

@@ -425,7 +427,7 @@ tuple ``Tuple[str, ...]`` and substituted in the return type.
425427

426428

427429
Unpacking unbounded tuples is useful in function signatures where we
428-
don't care about the exact elements and do not want to define an
430+
don't care about the exact elements and don't want to define an
429431
unnecessary ``TypeVarTuple``:
430432

431433
::
@@ -446,23 +448,24 @@ unnecessary ``TypeVarTuple``:
446448

447449
We can also pass a ``Tuple[int, ...]`` wherever a ``Tuple[*Ts]`` is
448450
expected. This is useful when we have particularly dynamic code and
449-
cannot infer the precise number of dimensions or the precise types for
451+
cannot state the precise number of dimensions or the precise types for
450452
each of the dimensions. In those cases, we can smoothly fall back to
451453
an unbounded tuple:
452454

453455
::
454456

457+
y: Array[*Tuple[Any, ...]] = read_from_file()
458+
455459
def expect_variadic_array(
456460
x: Array[Batch, *Shape]
457461
) -> None: ...
458462

463+
expect_variadic_array(y) # OK
464+
459465
def expect_precise_array(
460466
x: Array[Batch, Height, Width, Channels]
461467
) -> None: ...
462468

463-
y: Array[*Tuple[Any, ...]] = read_from_file()
464-
465-
expect_variadic_array(y) # OK
466469
expect_precise_array(y) # OK
467470

468471
``Array[*Tuple[Any, ...]]`` stands for an array with an arbitrary
@@ -473,10 +476,10 @@ is bound to ``Tuple[Any, ...]``. In the call to
473476
``Width``, and ``Channels`` are all bound to ``Any``.
474477

475478
This allows users to handle dynamic code gracefully while still
476-
explicitly marking the code as unsafe (by using ``*Tuple[Any, ...]``).
477-
Otherwise, users would face noisy errors from the type checker every
478-
time they tried to use the variable ``y``, which would hinder them
479-
when migrating a legacy code base to use ``TypeVarTuple``.
479+
explicitly marking the code as unsafe (by using ``y: Array[*Tuple[Any,
480+
...]]``). Otherwise, users would face noisy errors from the type
481+
checker every time they tried to use the variable ``y``, which would
482+
hinder them when migrating a legacy code base to use ``TypeVarTuple``.
480483

481484
``*args`` as a Type Variable Tuple
482485
----------------------------------
@@ -508,18 +511,19 @@ or suffixes of the variadic argument list. For example:
508511
::
509512

510513
# os.execle takes arguments 'path, arg0, arg1, ..., env'
511-
def execle(path: str, *args: *Tuple[*Ts, Mapping[str, str]]) -> None: ...
514+
def execle(path: str, *args: *Tuple[*Ts, Env]) -> None: ...
512515

513516
Note this this is different to
514517

515518
::
516519

517-
def execle(path: str, *args: *Ts, env: Mapping[str, str]) -> None: ...
520+
def execle(path: str, *args: *Ts, env: Env) -> None: ...
518521

519522
as this would make ``env`` a keyword-only argument.
520523

521-
Unpacking an unbounded tuple is equivalent to the PEP 484 behavior of
522-
``*args: int``, which accepts zero or more values of type ``int``:
524+
Unpacking an unbounded tuple is equivalent to the PEP 484 behavior
525+
[#pep-484-args]_ of ``*args: int``, which accepts zero or more values
526+
of type ``int``:
523527

524528
::
525529

@@ -587,10 +591,10 @@ Type variable tuples can also be used in the arguments section of a
587591
def __init__(
588592
self,
589593
target: Callable[[*Ts], None],
590-
args: Tuple[*Ts]
591-
): ...
594+
args: Tuple[*Ts],
595+
) -> None: ...
592596

593-
def func(arg1: int, arg2: str): ...
597+
def func(arg1: int, arg2: str) -> None: ...
594598

595599
Process(target=func, args=(0, 'foo')) # Valid
596600
Process(target=func, args=('foo', 0)) # Error
@@ -729,7 +733,7 @@ Normal ``TypeVar`` instances can also be used in such aliases:
729733
Foo[str, int]
730734
# T bound to float, Ts to Tuple[()]
731735
Foo[float]
732-
# T bound to Any, Ts to an arbitrary number of Any
736+
# T bound to Any, Ts to an Tuple[Any, ...]
733737
Foo
734738

735739
Overloads for Accessing Individual Types
@@ -1470,6 +1474,8 @@ References
14701474
14711475
.. [#dan-endorsement] https://mail.python.org/archives/list/python-dev@python.org/message/HTCARTYYCHETAMHB6OVRNR5EW5T2CP4J/
14721476
1477+
.. [#pep-484-args] https://www.python.org/dev/peps/pep-0484/#arbitrary-argument-lists-and-default-argument-values
1478+
14731479
Copyright
14741480
=========
14751481

0 commit comments

Comments
 (0)