Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5055,6 +5055,15 @@ All parameterized generics implement special read-only attributes.
have correct ``__parameters__`` after substitution because
:class:`typing.ParamSpec` is intended primarily for static type checking.


.. attribute:: genericalias.__unpacked__

A boolean that is true if the alias has been unpacked using the
``*`` operator (see :data:`~typing.TypeVarTuple`).

.. versionadded:: 3.11


.. seealso::

:pep:`484` - Type Hints
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ def __deepcopy__(self, memo):
self.assertEqual(copied.__args__, alias.__args__)
self.assertEqual(copied.__parameters__, alias.__parameters__)

def test_unpack(self):
alias = tuple[str, ...]
self.assertIs(alias.__unpacked__, False)
unpacked = (*alias,)[0]
self.assertIs(unpacked.__unpacked__, True)

def test_union(self):
a = typing.Union[list[int], list[str]]
self.assertEqual(a.__args__, (list[int], list[str]))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add an ``__unpacked__`` attribute to :class:`types.GenericAlias`. Patch by
Jelle Zijlstra.
2 changes: 2 additions & 0 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ ga_vectorcall(PyObject *self, PyObject *const *args,
static const char* const attr_exceptions[] = {
"__origin__",
"__args__",
"__unpacked__",
"__parameters__",
"__mro_entries__",
"__reduce_ex__", // needed so we don't look up object.__reduce_ex__
Expand Down Expand Up @@ -657,6 +658,7 @@ static PyMethodDef ga_methods[] = {
static PyMemberDef ga_members[] = {
{"__origin__", T_OBJECT, offsetof(gaobject, origin), READONLY},
{"__args__", T_OBJECT, offsetof(gaobject, args), READONLY},
{"__unpacked__", T_BOOL, offsetof(gaobject, starred), READONLY},
{0}
};

Expand Down