Skip to content

Commit d43f47b

Browse files
[3.13] gh-151955: Allow more ParamSpec bounds (#152122)
gh-151955: Allow more ParamSpec and TypeVarTuple bounds (#151956) (cherry picked from commit 0fb82b4)
1 parent c2bc0e3 commit d43f47b

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
@@ -10046,6 +10046,17 @@ def test_paramspec_in_nested_generics(self):
1004610046
self.assertEqual(G2[[int, str], float], list[C])
1004710047
self.assertEqual(G3[[int, str], float], list[C] | int)
1004810048

10049+
def test_paramspec_bound(self):
10050+
P = ParamSpec('P', bound=[int, str])
10051+
self.assertEqual(P.__bound__, [int, str])
10052+
P2 = ParamSpec('P2', bound=(int, str))
10053+
self.assertEqual(P2.__bound__, (int, str))
10054+
obj = object()
10055+
P3 = ParamSpec('P3', bound=obj)
10056+
self.assertIs(P3.__bound__, obj)
10057+
P4 = ParamSpec('P4')
10058+
self.assertIs(P4.__bound__, None)
10059+
1004910060
def test_paramspec_gets_copied(self):
1005010061
# bpo-46581
1005110062
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
@@ -1061,20 +1061,12 @@ paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
10611061
PyErr_SetString(PyExc_ValueError, "Variance cannot be specified with infer_variance.");
10621062
return NULL;
10631063
}
1064-
if (bound != NULL) {
1065-
bound = type_check(bound, "Bound must be a type.");
1066-
if (bound == NULL) {
1067-
return NULL;
1068-
}
1069-
}
10701064
PyObject *module = caller();
10711065
if (module == NULL) {
1072-
Py_XDECREF(bound);
10731066
return NULL;
10741067
}
10751068
PyObject *ps = (PyObject *)paramspec_alloc(
10761069
name, bound, default_value, covariant, contravariant, infer_variance, module);
1077-
Py_XDECREF(bound);
10781070
Py_DECREF(module);
10791071
return ps;
10801072
}

0 commit comments

Comments
 (0)