https://github.com/python/cpython/commit/04fd103713a3aa6052a9afbf4b3132d4e318d0ad
commit: 04fd103713a3aa6052a9afbf4b3132d4e318d0ad
branch: main
author: KotlinIsland <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-04-22T06:28:12-07:00
summary:
gh-148207: add additional keywords to `typing.TypeVarTuple` (#148212)
files:
A Misc/NEWS.d/next/Library/2026-04-07-12-37-53.gh-issue-148207.YhGem4.rst
M Doc/library/typing.rst
M Doc/whatsnew/3.15.rst
M Lib/test/test_typing.py
M Objects/clinic/typevarobject.c.h
M Objects/typevarobject.c
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 9150385bd5888d..9bc0a3caeee8bf 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -1980,7 +1980,7 @@ without the dedicated syntax, as documented below.
.. _typevartuple:
-.. class:: TypeVarTuple(name, *, default=typing.NoDefault)
+.. class:: TypeVarTuple(name, *, bound=None, covariant=False,
contravariant=False, infer_variance=False, default=typing.NoDefault)
Type variable tuple. A specialized form of :ref:`type variable <typevar>`
that enables *variadic* generics.
@@ -2090,6 +2090,24 @@ without the dedicated syntax, as documented below.
The name of the type variable tuple.
+ .. attribute:: __covariant__
+
+ Whether the type variable tuple has been explicitly marked as covariant.
+
+ .. versionadded:: 3.15
+
+ .. attribute:: __contravariant__
+
+ Whether the type variable tuple has been explicitly marked as
contravariant.
+
+ .. versionadded:: 3.15
+
+ .. attribute:: __infer_variance__
+
+ Whether the type variable tuple's variance should be inferred by type
checkers.
+
+ .. versionadded:: 3.15
+
.. attribute:: __default__
The default value of the type variable tuple, or
:data:`typing.NoDefault` if it
@@ -2116,6 +2134,11 @@ without the dedicated syntax, as documented below.
.. versionadded:: 3.13
+ Type variable tuples created with ``covariant=True`` or
+ ``contravariant=True`` can be used to declare covariant or contravariant
+ generic types. The ``bound`` argument is also accepted, similar to
+ :class:`TypeVar`, but its actual semantics are yet to be decided.
+
.. versionadded:: 3.11
.. versionchanged:: 3.12
@@ -2127,6 +2150,11 @@ without the dedicated syntax, as documented below.
Support for default values was added.
+ .. versionchanged:: 3.15
+
+ Added support for the ``bound``, ``covariant``, ``contravariant``, and
+ ``infer_variance`` parameters.
+
.. class:: ParamSpec(name, *, bound=None, covariant=False,
contravariant=False, default=typing.NoDefault)
Parameter specification variable. A specialized version of
@@ -2196,6 +2224,20 @@ without the dedicated syntax, as documented below.
The name of the parameter specification.
+ .. attribute:: __covariant__
+
+ Whether the parameter specification has been explicitly marked as
covariant.
+
+ .. attribute:: __contravariant__
+
+ Whether the parameter specification has been explicitly marked as
contravariant.
+
+ .. attribute:: __infer_variance__
+
+ Whether the parameter specification's variance should be inferred by
type checkers.
+
+ .. versionadded:: 3.12
+
.. attribute:: __default__
The default value of the parameter specification, or
:data:`typing.NoDefault` if it
diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst
index 9630df9aad3021..500797910edb90 100644
--- a/Doc/whatsnew/3.15.rst
+++ b/Doc/whatsnew/3.15.rst
@@ -1296,6 +1296,11 @@ typing
child classes of that class cannot inherit from other disjoint bases that are
not parent or child classes of ``C``. (Contributed by Jelle Zijlstra in
:gh:`148639`.)
+* :class:`~typing.TypeVarTuple` now accepts ``bound``, ``covariant``,
+ ``contravariant``, and ``infer_variance`` keyword arguments, matching the
+ interface of :class:`~typing.TypeVar` and :class:`~typing.ParamSpec`.
+ ``bound`` semantics remain undefined in the specification.
+
unicodedata
-----------
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 3fb974c517da7c..bfae83fdaf6bc0 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -780,7 +780,7 @@ def test_typevartuple_none(self):
self.assertIs(U_None.__default__, None)
self.assertIs(U_None.has_default(), True)
- class X[**Ts]: ...
+ class X[*Ts]: ...
Ts, = X.__type_params__
self.assertIs(Ts.__default__, NoDefault)
self.assertIs(Ts.has_default(), False)
@@ -1288,6 +1288,57 @@ def test_cannot_call_instance(self):
with self.assertRaises(TypeError):
Ts()
+ def test_default_variance(self):
+ Ts = TypeVarTuple('Ts')
+ self.assertIs(Ts.__covariant__, False)
+ self.assertIs(Ts.__contravariant__, False)
+ self.assertIs(Ts.__infer_variance__, False)
+ self.assertIsNone(Ts.__bound__)
+
+ def test_covariant(self):
+ Ts_co = TypeVarTuple('Ts_co', covariant=True)
+ self.assertIs(Ts_co.__covariant__, True)
+ self.assertIs(Ts_co.__contravariant__, False)
+ self.assertIs(Ts_co.__infer_variance__, False)
+
+ def test_contravariant(self):
+ Ts_contra = TypeVarTuple('Ts_contra', contravariant=True)
+ self.assertIs(Ts_contra.__covariant__, False)
+ self.assertIs(Ts_contra.__contravariant__, True)
+ self.assertIs(Ts_contra.__infer_variance__, False)
+
+ def test_infer_variance(self):
+ Ts = TypeVarTuple('Ts', infer_variance=True)
+ self.assertIs(Ts.__covariant__, False)
+ self.assertIs(Ts.__contravariant__, False)
+ self.assertIs(Ts.__infer_variance__, True)
+
+ def test_bound(self):
+ Ts_bound = TypeVarTuple('Ts_bound', bound=int)
+ self.assertIs(Ts_bound.__bound__, int)
+ Ts_no_bound = TypeVarTuple('Ts_no_bound')
+ self.assertIsNone(Ts_no_bound.__bound__)
+
+ def test_no_bivariant(self):
+ with self.assertRaises(ValueError):
+ TypeVarTuple('Ts', covariant=True, contravariant=True)
+
+ def test_cannot_combine_explicit_and_infer(self):
+ with self.assertRaises(ValueError):
+ TypeVarTuple('Ts', covariant=True, infer_variance=True)
+ with self.assertRaises(ValueError):
+ TypeVarTuple('Ts', contravariant=True, infer_variance=True)
+
+ def test_repr_with_variance(self):
+ Ts = TypeVarTuple('Ts')
+ self.assertEqual(repr(Ts), '~Ts')
+ Ts_co = TypeVarTuple('Ts_co', covariant=True)
+ self.assertEqual(repr(Ts_co), '+Ts_co')
+ Ts_contra = TypeVarTuple('Ts_contra', contravariant=True)
+ self.assertEqual(repr(Ts_contra), '-Ts_contra')
+ Ts_infer = TypeVarTuple('Ts_infer', infer_variance=True)
+ self.assertEqual(repr(Ts_infer), 'Ts_infer')
+
def test_unpacked_typevartuple_is_equal_to_itself(self):
Ts = TypeVarTuple('Ts')
self.assertEqual((*Ts,)[0], (*Ts,)[0])
@@ -1427,16 +1478,16 @@ def test_repr_is_correct(self):
class G1(Generic[*Ts]): pass
class G2(Generic[Unpack[Ts]]): pass
- self.assertEqual(repr(Ts), 'Ts')
+ self.assertEqual(repr(Ts), '~Ts')
- self.assertEqual(repr((*Ts,)[0]), 'typing.Unpack[Ts]')
- self.assertEqual(repr(Unpack[Ts]), 'typing.Unpack[Ts]')
+ self.assertEqual(repr((*Ts,)[0]), 'typing.Unpack[~Ts]')
+ self.assertEqual(repr(Unpack[Ts]), 'typing.Unpack[~Ts]')
- self.assertEqual(repr(tuple[*Ts]), 'tuple[typing.Unpack[Ts]]')
- self.assertEqual(repr(Tuple[Unpack[Ts]]),
'typing.Tuple[typing.Unpack[Ts]]')
+ self.assertEqual(repr(tuple[*Ts]), 'tuple[typing.Unpack[~Ts]]')
+ self.assertEqual(repr(Tuple[Unpack[Ts]]),
'typing.Tuple[typing.Unpack[~Ts]]')
- self.assertEqual(repr(*tuple[*Ts]), '*tuple[typing.Unpack[Ts]]')
- self.assertEqual(repr(Unpack[Tuple[Unpack[Ts]]]),
'typing.Unpack[typing.Tuple[typing.Unpack[Ts]]]')
+ self.assertEqual(repr(*tuple[*Ts]), '*tuple[typing.Unpack[~Ts]]')
+ self.assertEqual(repr(Unpack[Tuple[Unpack[Ts]]]),
'typing.Unpack[typing.Tuple[typing.Unpack[~Ts]]]')
def test_variadic_class_repr_is_correct(self):
Ts = TypeVarTuple('Ts')
@@ -1475,61 +1526,61 @@ def test_variadic_class_alias_repr_is_correct(self):
class A(Generic[Unpack[Ts]]): pass
B = A[*Ts]
- self.assertEndsWith(repr(B), 'A[typing.Unpack[Ts]]')
+ self.assertEndsWith(repr(B), 'A[typing.Unpack[~Ts]]')
self.assertEndsWith(repr(B[()]), 'A[()]')
self.assertEndsWith(repr(B[float]), 'A[float]')
self.assertEndsWith(repr(B[float, str]), 'A[float, str]')
C = A[Unpack[Ts]]
- self.assertEndsWith(repr(C), 'A[typing.Unpack[Ts]]')
+ self.assertEndsWith(repr(C), 'A[typing.Unpack[~Ts]]')
self.assertEndsWith(repr(C[()]), 'A[()]')
self.assertEndsWith(repr(C[float]), 'A[float]')
self.assertEndsWith(repr(C[float, str]), 'A[float, str]')
D = A[*Ts, int]
- self.assertEndsWith(repr(D), 'A[typing.Unpack[Ts], int]')
+ self.assertEndsWith(repr(D), 'A[typing.Unpack[~Ts], int]')
self.assertEndsWith(repr(D[()]), 'A[int]')
self.assertEndsWith(repr(D[float]), 'A[float, int]')
self.assertEndsWith(repr(D[float, str]), 'A[float, str, int]')
E = A[Unpack[Ts], int]
- self.assertEndsWith(repr(E), 'A[typing.Unpack[Ts], int]')
+ self.assertEndsWith(repr(E), 'A[typing.Unpack[~Ts], int]')
self.assertEndsWith(repr(E[()]), 'A[int]')
self.assertEndsWith(repr(E[float]), 'A[float, int]')
self.assertEndsWith(repr(E[float, str]), 'A[float, str, int]')
F = A[int, *Ts]
- self.assertEndsWith(repr(F), 'A[int, typing.Unpack[Ts]]')
+ self.assertEndsWith(repr(F), 'A[int, typing.Unpack[~Ts]]')
self.assertEndsWith(repr(F[()]), 'A[int]')
self.assertEndsWith(repr(F[float]), 'A[int, float]')
self.assertEndsWith(repr(F[float, str]), 'A[int, float, str]')
G = A[int, Unpack[Ts]]
- self.assertEndsWith(repr(G), 'A[int, typing.Unpack[Ts]]')
+ self.assertEndsWith(repr(G), 'A[int, typing.Unpack[~Ts]]')
self.assertEndsWith(repr(G[()]), 'A[int]')
self.assertEndsWith(repr(G[float]), 'A[int, float]')
self.assertEndsWith(repr(G[float, str]), 'A[int, float, str]')
H = A[int, *Ts, str]
- self.assertEndsWith(repr(H), 'A[int, typing.Unpack[Ts], str]')
+ self.assertEndsWith(repr(H), 'A[int, typing.Unpack[~Ts], str]')
self.assertEndsWith(repr(H[()]), 'A[int, str]')
self.assertEndsWith(repr(H[float]), 'A[int, float, str]')
self.assertEndsWith(repr(H[float, str]), 'A[int, float, str, str]')
I = A[int, Unpack[Ts], str]
- self.assertEndsWith(repr(I), 'A[int, typing.Unpack[Ts], str]')
+ self.assertEndsWith(repr(I), 'A[int, typing.Unpack[~Ts], str]')
self.assertEndsWith(repr(I[()]), 'A[int, str]')
self.assertEndsWith(repr(I[float]), 'A[int, float, str]')
self.assertEndsWith(repr(I[float, str]), 'A[int, float, str, str]')
J = A[*Ts, *tuple[str, ...]]
- self.assertEndsWith(repr(J), 'A[typing.Unpack[Ts], *tuple[str, ...]]')
+ self.assertEndsWith(repr(J), 'A[typing.Unpack[~Ts], *tuple[str, ...]]')
self.assertEndsWith(repr(J[()]), 'A[*tuple[str, ...]]')
self.assertEndsWith(repr(J[float]), 'A[float, *tuple[str, ...]]')
self.assertEndsWith(repr(J[float, str]), 'A[float, str, *tuple[str,
...]]')
K = A[Unpack[Ts], Unpack[Tuple[str, ...]]]
- self.assertEndsWith(repr(K), 'A[typing.Unpack[Ts],
typing.Unpack[typing.Tuple[str, ...]]]')
+ self.assertEndsWith(repr(K), 'A[typing.Unpack[~Ts],
typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K[()]), 'A[typing.Unpack[typing.Tuple[str,
...]]]')
self.assertEndsWith(repr(K[float]), 'A[float,
typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K[float, str]), 'A[float, str,
typing.Unpack[typing.Tuple[str, ...]]]')
@@ -1550,9 +1601,9 @@ class G(type(Unpack[Ts])): pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Unpack'):
class H(Unpack): pass
- with self.assertRaisesRegex(TypeError, r'Cannot subclass
typing.Unpack\[Ts\]'):
+ with self.assertRaisesRegex(TypeError, r'Cannot subclass
typing.Unpack\[~Ts\]'):
class I(*Ts): pass
- with self.assertRaisesRegex(TypeError, r'Cannot subclass
typing.Unpack\[Ts\]'):
+ with self.assertRaisesRegex(TypeError, r'Cannot subclass
typing.Unpack\[~Ts\]'):
class J(Unpack[Ts]): pass
def test_variadic_class_args_are_correct(self):
@@ -5596,13 +5647,13 @@ class TsP(Generic[*Ts, P]):
MyCallable[[int], bool]: "MyCallable[[int],
bool]",
MyCallable[[int, str], bool]: "MyCallable[[int, str],
bool]",
MyCallable[[int, list[int]], bool]: "MyCallable[[int,
list[int]], bool]",
- MyCallable[Concatenate[*Ts, P], T]:
"MyCallable[typing.Concatenate[typing.Unpack[Ts], ~P], ~T]",
+ MyCallable[Concatenate[*Ts, P], T]:
"MyCallable[typing.Concatenate[typing.Unpack[~Ts], ~P], ~T]",
DoubleSpec[P2, P, T]: "DoubleSpec[~P2, ~P,
~T]",
DoubleSpec[[int], [str], bool]: "DoubleSpec[[int],
[str], bool]",
DoubleSpec[[int, int], [str, str], bool]: "DoubleSpec[[int, int],
[str, str], bool]",
- TsP[*Ts, P]: "TsP[typing.Unpack[Ts],
~P]",
+ TsP[*Ts, P]: "TsP[typing.Unpack[~Ts],
~P]",
TsP[int, str, list[int], []]: "TsP[int, str,
list[int], []]",
TsP[int, [str, list[int]]]: "TsP[int, [str,
list[int]]]",
diff --git
a/Misc/NEWS.d/next/Library/2026-04-07-12-37-53.gh-issue-148207.YhGem4.rst
b/Misc/NEWS.d/next/Library/2026-04-07-12-37-53.gh-issue-148207.YhGem4.rst
new file mode 100644
index 00000000000000..dd88be0ad25d11
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-04-07-12-37-53.gh-issue-148207.YhGem4.rst
@@ -0,0 +1,3 @@
+:class:`typing.TypeVarTuple` now accepts ``bound``, ``covariant``,
+``contravariant``, and ``infer_variance`` parameters, matching the interface
+of :class:`typing.TypeVar` and :class:`typing.ParamSpec`.
diff --git a/Objects/clinic/typevarobject.c.h b/Objects/clinic/typevarobject.c.h
index bd4c7a0e64fd49..d2f350a3487f08 100644
--- a/Objects/clinic/typevarobject.c.h
+++ b/Objects/clinic/typevarobject.c.h
@@ -517,13 +517,15 @@ paramspec_has_default(PyObject *self, PyObject
*Py_UNUSED(ignored))
}
PyDoc_STRVAR(typevartuple__doc__,
-"typevartuple(name, *, default=typing.NoDefault)\n"
+"typevartuple(name, *, bound=None, covariant=False, contravariant=False,\n"
+" infer_variance=False, default=typing.NoDefault)\n"
"--\n"
"\n"
"Create a new TypeVarTuple with the given name.");
static PyObject *
-typevartuple_impl(PyTypeObject *type, PyObject *name,
+typevartuple_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
+ int covariant, int contravariant, int infer_variance,
PyObject *default_value);
static PyObject *
@@ -532,7 +534,7 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject
*kwargs)
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
- #define NUM_KEYWORDS 2
+ #define NUM_KEYWORDS 6
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
@@ -541,7 +543,7 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject
*kwargs)
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_hash = -1,
- .ob_item = { &_Py_ID(name), &_Py_ID(default), },
+ .ob_item = { &_Py_ID(name), &_Py_ID(bound), &_Py_ID(covariant),
&_Py_ID(contravariant), &_Py_ID(infer_variance), &_Py_ID(default), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
@@ -550,18 +552,22 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject
*kwargs)
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
- static const char * const _keywords[] = {"name", "default", NULL};
+ static const char * const _keywords[] = {"name", "bound", "covariant",
"contravariant", "infer_variance", "default", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "typevartuple",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
- PyObject *argsbuf[2];
+ PyObject *argsbuf[6];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
PyObject *name;
+ PyObject *bound = Py_None;
+ int covariant = 0;
+ int contravariant = 0;
+ int infer_variance = 0;
PyObject *default_value = &_Py_NoDefaultStruct;
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs,
kwargs, NULL, &_parser,
@@ -577,9 +583,42 @@ typevartuple(PyTypeObject *type, PyObject *args, PyObject
*kwargs)
if (!noptargs) {
goto skip_optional_kwonly;
}
- default_value = fastargs[1];
+ if (fastargs[1]) {
+ bound = fastargs[1];
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (fastargs[2]) {
+ covariant = PyObject_IsTrue(fastargs[2]);
+ if (covariant < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (fastargs[3]) {
+ contravariant = PyObject_IsTrue(fastargs[3]);
+ if (contravariant < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ if (fastargs[4]) {
+ infer_variance = PyObject_IsTrue(fastargs[4]);
+ if (infer_variance < 0) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_kwonly;
+ }
+ }
+ default_value = fastargs[5];
skip_optional_kwonly:
- return_value = typevartuple_impl(type, name, default_value);
+ return_value = typevartuple_impl(type, name, bound, covariant,
contravariant, infer_variance, default_value);
exit:
return return_value;
@@ -764,4 +803,4 @@ typealias_new(PyTypeObject *type, PyObject *args, PyObject
*kwargs)
exit:
return return_value;
}
-/*[clinic end generated code: output=67ab9a5d1869f2c9 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=2e7dd170924d92e5 input=a9049054013a1b77]*/
diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c
index c2b8ee43119cb1..cdc0ea42eac263 100644
--- a/Objects/typevarobject.c
+++ b/Objects/typevarobject.c
@@ -36,8 +36,12 @@ typedef struct {
typedef struct {
PyObject_HEAD
PyObject *name;
+ PyObject *bound;
PyObject *default_value;
PyObject *evaluate_default;
+ bool covariant;
+ bool contravariant;
+ bool infer_variance;
} typevartupleobject;
typedef struct {
@@ -1524,6 +1528,7 @@ typevartuple_dealloc(PyObject *self)
typevartupleobject *tvt = typevartupleobject_CAST(self);
Py_XDECREF(tvt->name);
+ Py_XDECREF(tvt->bound);
Py_XDECREF(tvt->default_value);
Py_XDECREF(tvt->evaluate_default);
PyObject_ClearManagedDict(self);
@@ -1555,16 +1560,28 @@ static PyObject *
typevartuple_repr(PyObject *self)
{
typevartupleobject *tvt = typevartupleobject_CAST(self);
- return Py_NewRef(tvt->name);
+
+ if (tvt->infer_variance) {
+ return Py_NewRef(tvt->name);
+ }
+
+ char variance = tvt->covariant ? '+' : tvt->contravariant ? '-' : '~';
+ return PyUnicode_FromFormat("%c%U", variance, tvt->name);
}
static PyMemberDef typevartuple_members[] = {
{"__name__", _Py_T_OBJECT, offsetof(typevartupleobject, name),
Py_READONLY},
+ {"__bound__", _Py_T_OBJECT, offsetof(typevartupleobject, bound),
Py_READONLY},
+ {"__covariant__", Py_T_BOOL, offsetof(typevartupleobject, covariant),
Py_READONLY},
+ {"__contravariant__", Py_T_BOOL, offsetof(typevartupleobject,
contravariant), Py_READONLY},
+ {"__infer_variance__", Py_T_BOOL, offsetof(typevartupleobject,
infer_variance), Py_READONLY},
{0}
};
static typevartupleobject *
-typevartuple_alloc(PyObject *name, PyObject *module, PyObject *default_value)
+typevartuple_alloc(PyObject *name, PyObject *bound, PyObject *default_value,
+ bool covariant, bool contravariant, bool infer_variance,
+ PyObject *module)
{
PyTypeObject *tp =
_PyInterpreterState_GET()->cached_objects.typevartuple_type;
typevartupleobject *tvt = PyObject_GC_New(typevartupleobject, tp);
@@ -1572,6 +1589,10 @@ typevartuple_alloc(PyObject *name, PyObject *module,
PyObject *default_value)
return NULL;
}
tvt->name = Py_NewRef(name);
+ tvt->bound = Py_XNewRef(bound);
+ tvt->covariant = covariant;
+ tvt->contravariant = contravariant;
+ tvt->infer_variance = infer_variance;
tvt->default_value = Py_XNewRef(default_value);
tvt->evaluate_default = NULL;
_PyObject_GC_TRACK(tvt);
@@ -1590,21 +1611,46 @@ typevartuple.__new__
name: object(subclass_of="&PyUnicode_Type")
*
+ bound: object = None
+ covariant: bool = False
+ contravariant: bool = False
+ infer_variance: bool = False
default as default_value: object(c_default="&_Py_NoDefaultStruct") =
typing.NoDefault
Create a new TypeVarTuple with the given name.
[clinic start generated code]*/
static PyObject *
-typevartuple_impl(PyTypeObject *type, PyObject *name,
+typevartuple_impl(PyTypeObject *type, PyObject *name, PyObject *bound,
+ int covariant, int contravariant, int infer_variance,
PyObject *default_value)
-/*[clinic end generated code: output=9d6b76dfe95aae51 input=e149739929a866d0]*/
+/*[clinic end generated code: output=40bc9ca10f64e392 input=56e28c725a8da40b]*/
{
+ if (covariant && contravariant) {
+ PyErr_SetString(PyExc_ValueError, "Bivariant types are not
supported.");
+ return NULL;
+ }
+ if (infer_variance && (covariant || contravariant)) {
+ PyErr_SetString(PyExc_ValueError, "Variance cannot be specified with
infer_variance.");
+ return NULL;
+ }
+ if (Py_IsNone(bound)) {
+ bound = 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 *result = (PyObject *)typevartuple_alloc(name, module,
default_value);
+ PyObject *result = (PyObject *)typevartuple_alloc(
+ name, bound, default_value, covariant, contravariant, infer_variance,
module);
+ Py_XDECREF(bound);
Py_DECREF(module);
return result;
}
@@ -1688,6 +1734,7 @@ typevartuple_traverse(PyObject *self, visitproc visit,
void *arg)
Py_VISIT(Py_TYPE(self));
typevartupleobject *tvt = typevartupleobject_CAST(self);
Py_VISIT(tvt->name);
+ Py_VISIT(tvt->bound);
Py_VISIT(tvt->default_value);
Py_VISIT(tvt->evaluate_default);
return PyObject_VisitManagedDict(self, visit, arg);
@@ -1698,6 +1745,7 @@ typevartuple_clear(PyObject *self)
{
typevartupleobject *tvt = typevartupleobject_CAST(self);
Py_CLEAR(tvt->name);
+ Py_CLEAR(tvt->bound);
Py_CLEAR(tvt->default_value);
Py_CLEAR(tvt->evaluate_default);
PyObject_ClearManagedDict(self);
@@ -1829,7 +1877,7 @@ PyObject *
_Py_make_typevartuple(PyThreadState *Py_UNUSED(ignored), PyObject *v)
{
assert(PyUnicode_Check(v));
- return (PyObject *)typevartuple_alloc(v, NULL, NULL);
+ return (PyObject *)typevartuple_alloc(v, NULL, NULL, false, false, true,
NULL);
}
static PyObject *
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]