Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0a91264
PEP 646: Implement support in typing.py
mrahtz Jan 30, 2022
33dc1bf
Fix typo
mrahtz Jan 30, 2022
51ea1af
Remove unnecessary whitespace
mrahtz Jan 30, 2022
fa133dd
Remove some old debug prints
mrahtz Jan 30, 2022
6d1126e
Re-implement Unpack using _SpecialForm
mrahtz Jan 30, 2022
51870e2
📜🤖 Added by blurb_it.
blurb-it[bot] Jan 30, 2022
bc12b49
Fix call to is_unpacked_typevartuple
mrahtz Feb 1, 2022
4a647cf
Update docstring for Unpack to reflect that it isn't just for backwar…
mrahtz Feb 6, 2022
e4e68a6
Fix _is_unpacked_typevartuple for argument of Unpack[tuple[...]]
mrahtz Feb 6, 2022
7fb4043
Add more tests for repr of variadic classes
mrahtz Feb 6, 2022
4b1b7f6
Add more tests to test_variadic_class_with_2_typevars_accepts_2_or_mo…
mrahtz Feb 6, 2022
c4351f9
test_typevar_substitution: Change to loop over test cases
mrahtz Feb 7, 2022
2398387
Merge remote-tracking branch 'python/main' into pep646-typing
mrahtz Feb 7, 2022
95625d1
Fix problems after pulling from main
mrahtz Feb 8, 2022
89cbdeb
_determine_typevar_substitution: Change 'params' to 'args'
mrahtz Feb 8, 2022
d4c487d
_determine_typevar_substitution: Add support for unpacked tuples
mrahtz Feb 8, 2022
ac4b5f9
Improve docstring for _BoundVarianceMixin
mrahtz Feb 14, 2022
fbf1a95
Remove arity checks for things involving a TypeVarTuple
mrahtz Feb 15, 2022
5e0fcab
Add basic __args__ tests
mrahtz Feb 15, 2022
6d86710
Merge branch 'main' into pep646-typing
mrahtz Feb 15, 2022
55f3a5d
fix lint in NEWS file
JelleZijlstra Feb 23, 2022
5212108
Make repr(A[]) == A[()] instead of A[]
mrahtz Feb 26, 2022
f2811a0
Add some extra repr tests
mrahtz Feb 26, 2022
f5f4440
Remove debug print
mrahtz Feb 26, 2022
45ff353
Make a couple of simplifications
mrahtz Feb 26, 2022
ba31d6e
Optimize is_typevar_like
mrahtz Feb 26, 2022
6789326
Refactor tests for variadic get_origin and get_origin into separate c…
mrahtz Feb 26, 2022
caf435e
Remove test for a parameterization that's actually invalid
mrahtz Feb 26, 2022
9835938
Move some tests from GetUtilitiesTestCase to TypeVarTupleTests
mrahtz Feb 26, 2022
ad5b090
Merge branch 'main' into pep646-typing
JelleZijlstra Mar 4, 2022
2604094
Remove accidental cosmetic change
mrahtz Mar 5, 2022
75cd9d0
Add some type annotations
mrahtz Mar 5, 2022
23b393b
Fix various things in Generic.__class_getitem__
mrahtz Mar 5, 2022
f094905
Split up _determine_typevar_substitution
mrahtz Mar 5, 2022
b9b1c80
Cut out variadic type substitution logic
mrahtz Mar 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update docstring for Unpack to reflect that it isn't just for backwar…
…ds compatibility now
  • Loading branch information
mrahtz committed Feb 6, 2022
commit 4a647cf59cef167761c491d5ede51a04a8eec9b2
30 changes: 20 additions & 10 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,23 +1496,33 @@ def copy_with(self, params):

@_SpecialForm
def Unpack(self, parameters):
"""Implementation of the type unpack operator for older versions of Python.
"""Type unpack operator.

The type unpack operator takes the child types from some container type
such as `Tuple[int, str]` and "pulls them out". For example,
`Dict[Unpack[Tuple[int, str]]` is equivalent to `Dict[int, str]`.
In newer versions of Python, this is implemented using the `*` operator.
The type unpack operator takes the child types from some container type,
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
example:

The type unpack operator can be applied to a `TypeVarTuple` instance:
# For some generic class `Foo`:
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]

Ts = TypeVarTuple('Ts')
Tuple[Unpack[Ts]] # Equivalent to Tuple[*Ts]
# Specifies that `Bar` is generic in an arbitrary number of types.
# (Think of `Ts` as a tuple of an arbitrary number of individual
# `TypeVar`s, which the `Unpack` is 'pulling out' directly into the
# `Generic[]`.)
class Bar(Generic[Unpack[Ts]]): ...
Bar[int] # Valid
Bar[int, str] # Also valid

Or to a parameterised `Tuple`:
From Python 3.11, this can also be done using the `*` operator:

Tuple[Unpack[Tuple[int, str]]] # Equivalent to Tuple[*Tuple[int, str]]
Foo[*tuple[int, str]]
class Bar(Generic[*Ts]): ...

There is no runtime checking of this operator.
Note that there is only some runtime checking of this operator. Not
everything the runtime allows may be accepted by static type checkers.

For more information, see PEP 646.
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _UnpackGenericAlias(origin=self, params=(item,))
Expand Down