from test.typinganndata import mod_generics_cache
from typing import (
- Callable, TypeAliasType, TypeVar, TypeVarTuple, ParamSpec, get_args,
+ Callable, TypeAliasType, TypeVar, TypeVarTuple, ParamSpec, Unpack, get_args,
)
self.assertEqual(mod_generics_cache.OldStyle.__module__,
mod_generics_cache.__name__)
+ def test_unpack(self):
+ type Alias = tuple[int, int]
+ unpacked = (*Alias,)[0]
+ self.assertEqual(unpacked, Unpack[Alias])
+
+ class Foo[*Ts]:
+ pass
+
+ x = Foo[str, *Alias]
+ self.assertEqual(x.__args__, (str, Unpack[Alias]))
+
# All these type aliases are used for pickling tests:
T = TypeVar('T')
-// TypeVar, TypeVarTuple, and ParamSpec
+// TypeVar, TypeVarTuple, ParamSpec, and TypeAlias
#include "Python.h"
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK, PyAnnotateFormat
#include "pycore_typevarobject.h"
}
static PyObject *
-typevartuple_unpack(PyObject *tvt)
+unpack(PyObject *self)
{
PyObject *typing = PyImport_ImportModule("typing");
if (typing == NULL) {
Py_DECREF(typing);
return NULL;
}
- PyObject *unpacked = PyObject_GetItem(unpack, tvt);
+ PyObject *unpacked = PyObject_GetItem(unpack, self);
Py_DECREF(typing);
Py_DECREF(unpack);
return unpacked;
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *param = PyTuple_GET_ITEM(params, i);
if (Py_IS_TYPE(param, tp)) {
- PyObject *unpacked = typevartuple_unpack(param);
+ PyObject *unpacked = unpack(param);
if (unpacked == NULL) {
Py_DECREF(new_params);
return NULL;
}
static PyObject *
-typevartuple_iter(PyObject *self)
+unpack_iter(PyObject *self)
{
- PyObject *unpacked = typevartuple_unpack(self);
+ PyObject *unpacked = unpack(self);
if (unpacked == NULL) {
return NULL;
}
{Py_tp_methods, typevartuple_methods},
{Py_tp_getset, typevartuple_getset},
{Py_tp_new, typevartuple},
- {Py_tp_iter, typevartuple_iter},
+ {Py_tp_iter, unpack_iter},
{Py_tp_repr, typevartuple_repr},
{Py_tp_dealloc, typevartuple_dealloc},
{Py_tp_alloc, PyType_GenericAlloc},
.tp_dealloc = typealias_dealloc,
.tp_new = typealias_new,
.tp_free = PyObject_GC_Del,
+ .tp_iter = unpack_iter,
.tp_traverse = typealias_traverse,
.tp_clear = typealias_clear,
.tp_repr = typealias_repr,