From 08575c3e5c19bef2ade448e10cb4623651b9d43e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 24 Jun 2026 09:10:10 -0700 Subject: [PATCH] gh-151955: Allow more ParamSpec and TypeVarTuple bounds (#151956) (cherry picked from commit 0fb82b46df7bc95c2ba816afba56ff6a6c51caaf) --- Lib/test/test_typing.py | 11 +++++++++++ .../2026-06-22-18-01-00.gh-issue-151955.6u5iwm.rst | 2 ++ Objects/typevarobject.c | 8 -------- 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-22-18-01-00.gh-issue-151955.6u5iwm.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 1832af632de4dff..48903f36af12c7d 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -10346,6 +10346,17 @@ def test_paramspec_in_nested_generics(self): self.assertEqual(G2[[int, str], float], list[C]) self.assertEqual(G3[[int, str], float], list[C] | int) + def test_paramspec_bound(self): + P = ParamSpec('P', bound=[int, str]) + self.assertEqual(P.__bound__, [int, str]) + P2 = ParamSpec('P2', bound=(int, str)) + self.assertEqual(P2.__bound__, (int, str)) + obj = object() + P3 = ParamSpec('P3', bound=obj) + self.assertIs(P3.__bound__, obj) + P4 = ParamSpec('P4') + self.assertIs(P4.__bound__, None) + def test_paramspec_gets_copied(self): # bpo-46581 P = ParamSpec('P') diff --git a/Misc/NEWS.d/next/Library/2026-06-22-18-01-00.gh-issue-151955.6u5iwm.rst b/Misc/NEWS.d/next/Library/2026-06-22-18-01-00.gh-issue-151955.6u5iwm.rst new file mode 100644 index 000000000000000..243b3308a57ad32 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-22-18-01-00.gh-issue-151955.6u5iwm.rst @@ -0,0 +1,2 @@ +Allow more types to be used in the ``bound`` argument to +:class:`typing.ParamSpec`. diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 80d3fd488afae21..66a14cc30fecb87 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -1334,20 +1334,12 @@ paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound, PyErr_SetString(PyExc_ValueError, "Variance cannot be specified with infer_variance."); return NULL; } - if (bound != NULL) { - bound = type_check(bound, "Bound must be a type."); - if (bound == NULL) { - return NULL; - } - } PyObject *module = caller(); if (module == NULL) { - Py_XDECREF(bound); return NULL; } PyObject *ps = (PyObject *)paramspec_alloc( name, bound, default_value, covariant, contravariant, infer_variance, module); - Py_XDECREF(bound); Py_DECREF(module); return ps; }