From 63d2d80faa3a26f8380d0f771a85fbda6b6829ce Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 29 Apr 2022 16:45:16 -0600 Subject: [PATCH 1/2] gh-87390: Add __unpacked__ attribute to types.GenericAlias --- Doc/library/types.rst | 19 +++++++++++++++++++ Lib/test/test_genericalias.py | 6 ++++++ ...2-04-29-16-41-08.gh-issue-87390.3LNNCv.rst | 2 ++ Objects/genericaliasobject.c | 2 ++ 4 files changed, 29 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-29-16-41-08.gh-issue-87390.3LNNCv.rst diff --git a/Doc/library/types.rst b/Doc/library/types.rst index e0e77dfbfe7ed2..a77fdfb5959306 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -311,6 +311,25 @@ Standard names are defined for the following types: .. versionchanged:: 3.9.2 This type can now be subclassed. + .. attribute:: __origin__ + + The non-parameterized generic class. + + .. attribute:: __args__ + + The :class:`tuple` of types which parameterize the generic class. + + .. attribute:: __parameters__ + + The :data:`~typing.TypeVar`\ s which serve as type parameters for + the generic class. + + .. attribute:: __unpacked__ + + A boolean that is true if the alias has been unpacked using the + ``*`` operator (see :data:`~typing.TypeVarTuple`). + + .. versionadded:: 3.11 .. class:: UnionType diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index bf96ba065fbb04..cd6a3abda3a1ff 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -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])) diff --git a/Misc/NEWS.d/next/Library/2022-04-29-16-41-08.gh-issue-87390.3LNNCv.rst b/Misc/NEWS.d/next/Library/2022-04-29-16-41-08.gh-issue-87390.3LNNCv.rst new file mode 100644 index 00000000000000..c368c1eb3be18b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-29-16-41-08.gh-issue-87390.3LNNCv.rst @@ -0,0 +1,2 @@ +Add an ``__unpacked__`` attribute to :class:`types.GenericAlias`. Patch by +Jelle Zijlstra. diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index 7059c4018303e0..fb88907f4616e5 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -426,6 +426,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__ @@ -567,6 +568,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} }; From 81cbc83ebaa450d2a7232281a7239cbe969d5566 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 2 May 2022 08:53:49 -0600 Subject: [PATCH 2/2] move to the existing docs --- Doc/library/stdtypes.rst | 9 +++++++++ Doc/library/types.rst | 19 ------------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 764d83a6800d15..44447400c29bca 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -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 diff --git a/Doc/library/types.rst b/Doc/library/types.rst index a77fdfb5959306..e0e77dfbfe7ed2 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -311,25 +311,6 @@ Standard names are defined for the following types: .. versionchanged:: 3.9.2 This type can now be subclassed. - .. attribute:: __origin__ - - The non-parameterized generic class. - - .. attribute:: __args__ - - The :class:`tuple` of types which parameterize the generic class. - - .. attribute:: __parameters__ - - The :data:`~typing.TypeVar`\ s which serve as type parameters for - the generic class. - - .. attribute:: __unpacked__ - - A boolean that is true if the alias has been unpacked using the - ``*`` operator (see :data:`~typing.TypeVarTuple`). - - .. versionadded:: 3.11 .. class:: UnionType