[Python-checkins] bpo-44791: Accept ellipsis as the last argument of typing.Concatenate (#30969)

JelleZijlstra webhook-mailer at python.org
Fri Apr 29 17:01:52 EDT 2022


https://github.com/python/cpython/commit/81120b6754c67a8f1f00cbc3af6963c0e1945911
commit: 81120b6754c67a8f1f00cbc3af6963c0e1945911
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-04-29T15:01:40-06:00
summary:

bpo-44791: Accept ellipsis as the last argument of typing.Concatenate (#30969)

files:
A Misc/NEWS.d/next/Library/2022-01-27-14-46-15.bpo-44791.tR1JFG.rst
M Doc/library/typing.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index d4a6f32aa36d2..0de380551c9fb 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -852,7 +852,8 @@ These can be used as types in annotations using ``[]``, each having a unique syn
    callable.  Usage is in the form
    ``Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable]``. ``Concatenate``
    is currently only valid when used as the first argument to a :data:`Callable`.
-   The last parameter to ``Concatenate`` must be a :class:`ParamSpec`.
+   The last parameter to ``Concatenate`` must be a :class:`ParamSpec` or
+   ellipsis (``...``).
 
    For example, to annotate a decorator ``with_lock`` which provides a
    :class:`threading.Lock` to the decorated function,  ``Concatenate`` can be
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index dab549b17e149..929f0df960ae0 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1755,8 +1755,7 @@ def test_concatenate(self):
         self.assertEqual(C[[], int], Callable[[int], int])
         self.assertEqual(C[Concatenate[str, P2], int],
                          Callable[Concatenate[int, str, P2], int])
-        with self.assertRaises(TypeError):
-            C[..., int]
+        self.assertEqual(C[..., int], Callable[Concatenate[int, ...], int])
 
         C = Callable[Concatenate[int, P], int]
         self.assertEqual(repr(C),
@@ -1767,8 +1766,7 @@ def test_concatenate(self):
         self.assertEqual(C[[]], Callable[[int], int])
         self.assertEqual(C[Concatenate[str, P2]],
                          Callable[Concatenate[int, str, P2], int])
-        with self.assertRaises(TypeError):
-            C[...]
+        self.assertEqual(C[...], Callable[Concatenate[int, ...], int])
 
     def test_errors(self):
         Callable = self.Callable
@@ -6739,8 +6737,7 @@ def test_var_substitution(self):
         self.assertEqual(C[int, []], (int,))
         self.assertEqual(C[int, Concatenate[str, P2]],
                          Concatenate[int, str, P2])
-        with self.assertRaises(TypeError):
-            C[int, ...]
+        self.assertEqual(C[int, ...], Concatenate[int, ...])
 
         C = Concatenate[int, P]
         self.assertEqual(C[P2], Concatenate[int, P2])
@@ -6748,8 +6745,7 @@ def test_var_substitution(self):
         self.assertEqual(C[str, float], (int, str, float))
         self.assertEqual(C[[]], (int,))
         self.assertEqual(C[Concatenate[str, P2]], Concatenate[int, str, P2])
-        with self.assertRaises(TypeError):
-            C[...]
+        self.assertEqual(C[...], Concatenate[int, ...])
 
 class TypeGuardTests(BaseTestCase):
     def test_basics(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index f4d4fa4d6713c..b250f2992dfff 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -714,9 +714,9 @@ def Concatenate(self, parameters):
         raise TypeError("Cannot take a Concatenate of no types.")
     if not isinstance(parameters, tuple):
         parameters = (parameters,)
-    if not isinstance(parameters[-1], ParamSpec):
+    if not (parameters[-1] is ... or isinstance(parameters[-1], ParamSpec)):
         raise TypeError("The last parameter to Concatenate should be a "
-                        "ParamSpec variable.")
+                        "ParamSpec variable or ellipsis.")
     msg = "Concatenate[arg, ...]: each arg must be a type."
     parameters = (*(_type_check(p, msg) for p in parameters[:-1]), parameters[-1])
     return _ConcatenateGenericAlias(self, parameters,
@@ -1641,9 +1641,6 @@ def copy_with(self, params):
             return (*params[:-1], *params[-1])
         if isinstance(params[-1], _ConcatenateGenericAlias):
             params = (*params[:-1], *params[-1].__args__)
-        elif not isinstance(params[-1], ParamSpec):
-            raise TypeError("The last parameter to Concatenate should be a "
-                            "ParamSpec variable.")
         return super().copy_with(params)
 
 
diff --git a/Misc/NEWS.d/next/Library/2022-01-27-14-46-15.bpo-44791.tR1JFG.rst b/Misc/NEWS.d/next/Library/2022-01-27-14-46-15.bpo-44791.tR1JFG.rst
new file mode 100644
index 0000000000000..31c6dcc058e94
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-27-14-46-15.bpo-44791.tR1JFG.rst
@@ -0,0 +1 @@
+Accept ellipsis as the last argument of :data:`typing.Concatenate`.



More information about the Python-checkins mailing list