Skip to content

Commit 0fb82b4

Browse files
gh-151955: Allow more ParamSpec and TypeVarTuple bounds (#151956)
1 parent ce8b81f commit 0fb82b4

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

Lib/test/test_typing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,11 @@ def test_infer_variance(self):
13161316
def test_bound(self):
13171317
Ts_bound = TypeVarTuple('Ts_bound', bound=int)
13181318
self.assertIs(Ts_bound.__bound__, int)
1319+
Ts_tuple_bound = TypeVarTuple('Ts_tuple_bound', bound=(int, str))
1320+
self.assertEqual(Ts_tuple_bound.__bound__, (int, str))
1321+
obj = object()
1322+
Ts_object = TypeVarTuple('Ts_object', bound=obj)
1323+
self.assertIs(Ts_object.__bound__, obj)
13191324
Ts_no_bound = TypeVarTuple('Ts_no_bound')
13201325
self.assertIsNone(Ts_no_bound.__bound__)
13211326

@@ -10534,6 +10539,17 @@ def test_paramspec_in_nested_generics(self):
1053410539
self.assertEqual(G2[[int, str], float], list[C])
1053510540
self.assertEqual(G3[[int, str], float], list[C] | int)
1053610541

10542+
def test_paramspec_bound(self):
10543+
P = ParamSpec('P', bound=[int, str])
10544+
self.assertEqual(P.__bound__, [int, str])
10545+
P2 = ParamSpec('P2', bound=(int, str))
10546+
self.assertEqual(P2.__bound__, (int, str))
10547+
obj = object()
10548+
P3 = ParamSpec('P3', bound=obj)
10549+
self.assertIs(P3.__bound__, obj)
10550+
P4 = ParamSpec('P4')
10551+
self.assertIs(P4.__bound__, None)
10552+
1053710553
def test_paramspec_gets_copied(self):
1053810554
# bpo-46581
1053910555
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` and :class:`typing.TypeVarTuple`.

Objects/typevarobject.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,20 +1340,12 @@ paramspec_new_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
13401340
PyErr_SetString(PyExc_ValueError, "Variance cannot be specified with infer_variance.");
13411341
return NULL;
13421342
}
1343-
if (bound != NULL) {
1344-
bound = type_check(bound, "Bound must be a type.");
1345-
if (bound == NULL) {
1346-
return NULL;
1347-
}
1348-
}
13491343
PyObject *module = caller();
13501344
if (module == NULL) {
1351-
Py_XDECREF(bound);
13521345
return NULL;
13531346
}
13541347
PyObject *ps = (PyObject *)paramspec_alloc(
13551348
name, bound, default_value, covariant, contravariant, infer_variance, module);
1356-
Py_XDECREF(bound);
13571349
Py_DECREF(module);
13581350
return ps;
13591351
}
@@ -1634,23 +1626,12 @@ typevartuple_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
16341626
PyErr_SetString(PyExc_ValueError, "Variance cannot be specified with infer_variance.");
16351627
return NULL;
16361628
}
1637-
if (Py_IsNone(bound)) {
1638-
bound = NULL;
1639-
}
1640-
if (bound != NULL) {
1641-
bound = type_check(bound, "Bound must be a type.");
1642-
if (bound == NULL) {
1643-
return NULL;
1644-
}
1645-
}
16461629
PyObject *module = caller();
16471630
if (module == NULL) {
1648-
Py_XDECREF(bound);
16491631
return NULL;
16501632
}
16511633
PyObject *result = (PyObject *)typevartuple_alloc(
16521634
name, bound, default_value, covariant, contravariant, infer_variance, module);
1653-
Py_XDECREF(bound);
16541635
Py_DECREF(module);
16551636
return result;
16561637
}

0 commit comments

Comments
 (0)