]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-87390: Add __unpacked__ attribute to types.GenericAlias (#92059)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Mon, 2 May 2022 19:21:59 +0000 (13:21 -0600)
committerGitHub <noreply@github.com>
Mon, 2 May 2022 19:21:59 +0000 (13:21 -0600)
Doc/library/stdtypes.rst
Lib/test/test_genericalias.py
Misc/NEWS.d/next/Library/2022-04-29-16-41-08.gh-issue-87390.3LNNCv.rst [new file with mode: 0644]
Objects/genericaliasobject.c

index 764d83a6800d15699e32c2e33a022e314382943d..44447400c29bca9d05429031747a755142a7ef21 100644 (file)
@@ -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
index 635ac0f7a85d7d3366d16211f694751ca7cde8eb..2d2adc11e1594bc1fa5495dcd37782bd44c120a7 100644 (file)
@@ -418,6 +418,12 @@ class BaseTest(unittest.TestCase):
                 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 (file)
index 0000000..c368c1e
--- /dev/null
@@ -0,0 +1,2 @@
+Add an ``__unpacked__`` attribute to :class:`types.GenericAlias`. Patch by
+Jelle Zijlstra.
index 7b689912dffc1b428b1a77a685d604d50cb28bce..c6ed1611bd29ec18d362f3afa13d5adb7af1a08b 100644 (file)
@@ -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__
@@ -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}
 };