@@ -1305,20 +1305,25 @@ These are not used in annotations. They are building blocks for creating generic
1305
1305
T = TypeVar('T')
1306
1306
Ts = TypeVarTuple('Ts')
1307
1307
1308
- def remove_first_element (tup: tuple[T, *Ts]) -> tuple[*Ts]:
1309
- return tup[1:]
1308
+ def move_first_element_to_last (tup: tuple[T, *Ts]) -> tuple[*Ts, T ]:
1309
+ return (* tup[1:], tup[0])
1310
1310
1311
1311
# T is bound to int, Ts is bound to ()
1312
- # Return value is (), which has type tuple[() ]
1313
- remove_first_element (tup=(1,))
1312
+ # Return value is (1, ), which has type tuple[int ]
1313
+ move_first_element_to_last (tup=(1,))
1314
1314
1315
1315
# T is bound to int, Ts is bound to (str,)
1316
- # Return value is ('spam',), which has type tuple[str]
1317
- remove_first_element (tup=(1, 'spam'))
1316
+ # Return value is ('spam', 1 ), which has type tuple[str, int ]
1317
+ move_first_element_to_last (tup=(1, 'spam'))
1318
1318
1319
1319
# T is bound to int, Ts is bound to (str, float)
1320
- # Return value is ('spam', 3.0), which has type tuple[str, float]
1321
- remove_first_element(tup=(1, 'spam', 3.0))
1320
+ # Return value is ('spam', 3.0, 1), which has type tuple[str, float, int]
1321
+ move_first_element_to_last(tup=(1, 'spam', 3.0))
1322
+
1323
+ # This fails to type check (and fails at runtime)
1324
+ # because tuple[()] is not compatible with tuple[T, *Ts]
1325
+ # (at least one element is required)
1326
+ move_first_element_to_last(tup=())
1322
1327
1323
1328
Note the use of the unpacking operator ``* `` in ``tuple[T, *Ts] ``.
1324
1329
Conceptually, you can think of ``Ts `` as a tuple of type variables
0 commit comments