Skip to content

Commit 08575c3

Browse files
committed
gh-151955: Allow more ParamSpec and TypeVarTuple bounds (#151956)
(cherry picked from commit 0fb82b4)
1 parent e1323cd commit 08575c3

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

Lib/test/test_typing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10346,6 +10346,17 @@ def test_paramspec_in_nested_generics(self):
1034610346
self.assertEqual(G2[[int, str], float], list[C])
1034710347
self.assertEqual(G3[[int, str], float], list[C] | int)
1034810348

10349+
def test_paramspec_bound(self):
10350+
P = ParamSpec('P', bound=[int, str])
10351+
self.assertEqual(P.__bound__, [int, str])
10352+
P2 = ParamSpec('P2', bound=(int, str))
10353+
self.assertEqual(P2.__bound__, (int, str))
10354+
obj = object()
10355+
P3 = ParamSpec('P3', bound=obj)
10356+
self.assertIs(P3.__bound__, obj)
10357+
P4 = ParamSpec('P4')
10358+
self.assertIs(P4.__bound__, None)
10359+
1034910360
def test_paramspec_gets_copied(self):
1035010361
# bpo-46581
1035110362
P = ParamSpec('P')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow more types to be used in the ``bound`` argument to
2+
:class:`typing.ParamSpec`.

Objects/typevarobject.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,20 +1334,12 @@ paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
13341334
PyErr_SetString(PyExc_ValueError, "Variance cannot be specified with infer_variance.");
13351335
return NULL;
13361336
}
1337-
if (bound != NULL) {
1338-
bound = type_check(bound, "Bound must be a type.");
1339-
if (bound == NULL) {
1340-
return NULL;
1341-
}
1342-
}
13431337
PyObject *module = caller();
13441338
if (module == NULL) {
1345-
Py_XDECREF(bound);
13461339
return NULL;
13471340
}
13481341
PyObject *ps = (PyObject *)paramspec_alloc(
13491342
name, bound, default_value, covariant, contravariant, infer_variance, module);
1350-
Py_XDECREF(bound);
13511343
Py_DECREF(module);
13521344
return ps;
13531345
}

0 commit comments

Comments
 (0)