Skip to content

Commit cebfa33

Browse files
authored
PEP 646: Add additional motivation for why *Ts over Unpack[Ts] (#2054)
1 parent b89333c commit cebfa33

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

pep-0646.rst

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ This PEP requires two grammar changes. Full diffs of ``python.gram``
740740
and simple tests to confirm correct behaviour are available at
741741
https://github.com/mrahtz/cpython/commits/pep646-grammar.
742742

743-
Star expressions in indexes
744-
---------------------------
743+
Change 1: Star Expressions in Indexes
744+
-------------------------------------
745745

746746
The first grammar change enables use of star expressions in index operations (that is,
747747
within square brackets), necessary to support star-unpacking of TypeVarTuples:
@@ -844,8 +844,8 @@ However, note that slices involving starred expressions are still invalid:
844844
array[*idxs_start:*idxs_end]
845845

846846

847-
``*args`` as a TypeVarTuple
848-
---------------------------
847+
Change 2: ``*args`` as a TypeVarTuple
848+
-------------------------------------
849849

850850
The second change enables use of ``*args: *Ts`` in function definitions.
851851

@@ -922,16 +922,33 @@ annotation is possible:
922922
Again, prevention of such annotations will need to be done by, say, static
923923
checkers, rather than at the level of syntax.
924924

925-
Alternatives
926-
------------
927-
928-
If these two grammar changes are considered too burdensome, there are two alternatives:
929-
930-
1. **Support change 1 but not change 2**. Starred expressions within indexes are
931-
more important to us than the ability to annotation ``*args``.
932-
2. **Use ``Unpack`` instead** - though in this case it might be better for us to
933-
reconsider our options to see whether there isn't another option which would be
934-
more readable.
925+
Alternatives (Why Not Just Use ``Unpack``?)
926+
-------------------------------------------
927+
928+
If these grammar changes are considered too burdensome, there are two
929+
alternatives.
930+
931+
The first would be to **support change 1 but not change 2**. Variadic generics
932+
are more important to us than the ability to annotate ``*args``.
933+
934+
The second alternative would be to **use ``Unpack`` instead**, requiring no
935+
grammar changes. However, we regard this as a suboptimal solution for two
936+
reasons:
937+
938+
* **Readability**. ``class Array(Generic[DType, Unpack[Shape]])`` is a bit
939+
of a mouthful; the flow of reading is interrupted by length of ``Unpack`` and
940+
the extra set of square brackets. ``class Array(Generic[DType, *Shape])``
941+
is much easier to skim, while still marking ``Shape`` as special.
942+
* **Intuitiveness**. We think a user is more likely to intuitively understand
943+
the meaning of ``*Ts`` - especially when they see that ``Ts`` is a
944+
TypeVar**Tuple** - than the meaning of ``Unpack[Ts]``. (This assumes
945+
the user is familiar with star-unpacking in other contexts; if the
946+
user is reading or writing code that uses variadic generics, this seems
947+
reasonable.)
948+
949+
If even change 1 is thought too significant a change, therefore, it might be
950+
better for us to reconsider our options before going ahead with this second
951+
alternative.
935952

936953
Backwards Compatibility
937954
=======================

0 commit comments

Comments
 (0)