@@ -11,9 +11,10 @@ class paramspec "paramspecobject *" "&_PyParamSpec_Type"
1111class paramspecargs "paramspecargsobject *" "&_PyParamSpecArgs_Type"
1212class paramspeckwargs "paramspeckwargsobject *" "&_PyParamSpecKwargs_Type"
1313class typevartuple "typevartupleobject *" "&_PyTypeVarTuple_Type"
14+ class typealias "typealiasobject *" "&_PyTypeAlias_Type"
1415class Generic "PyObject *" "&PyGeneric_Type"
1516[clinic start generated code]*/
16- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b0f1a94d4a27c0c ]*/
17+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a775b1d0f0b88d23 ]*/
1718
1819typedef struct {
1920 PyObject_HEAD
@@ -39,6 +40,14 @@ typedef struct {
3940 bool autovariance ;
4041} paramspecobject ;
4142
43+ typedef struct {
44+ PyObject_HEAD
45+ const char * name ;
46+ PyObject * type_params ;
47+ PyObject * compute_value ;
48+ PyObject * value ;
49+ } typealiasobject ;
50+
4251#include "clinic/typevarobject.c.h"
4352
4453static PyObject * call_typing_func_object (const char * name , PyObject * args ) {
@@ -1030,6 +1039,111 @@ PyObject *_Py_make_typevartuple(const char *name) {
10301039 return (PyObject * )typevartupleobject_alloc (name );
10311040}
10321041
1042+ static void
1043+ typealias_dealloc (PyObject * self )
1044+ {
1045+ typealiasobject * ta = (typealiasobject * )self ;
1046+ free ((void * )ta -> name );
1047+ Py_XDECREF (ta -> type_params );
1048+ Py_XDECREF (ta -> compute_value );
1049+ Py_XDECREF (ta -> value );
1050+ Py_TYPE (self )-> tp_free (self );
1051+ }
1052+
1053+ static PyObject *
1054+ typealias_repr (PyObject * self )
1055+ {
1056+ typealiasobject * ta = (typealiasobject * )self ;
1057+ return PyUnicode_FromFormat ("%s" , ta -> name );
1058+ }
1059+
1060+ static PyMemberDef typealias_members [] = {
1061+ {"__name__" , T_STRING , offsetof(typealiasobject , name ), READONLY },
1062+ {"__type_params__" , T_OBJECT , offsetof(typealiasobject , type_params ), READONLY },
1063+ {0 }
1064+ };
1065+
1066+ static typealiasobject *
1067+ typealias_alloc (const char * name , PyObject * type_params , PyObject * compute_value )
1068+ {
1069+ typealiasobject * ta = PyObject_GC_New (typealiasobject , & _PyTypeAlias_Type );
1070+ if (ta == NULL ) {
1071+ return NULL ;
1072+ }
1073+ ta -> name = strdup (name );
1074+ if (ta -> name == NULL ) {
1075+ Py_DECREF (ta );
1076+ return NULL ;
1077+ }
1078+ ta -> type_params = Py_XNewRef (type_params );
1079+ ta -> compute_value = Py_NewRef (compute_value );
1080+ ta -> value = NULL ;
1081+ PyObject_GC_Track (ta );
1082+ return ta ;
1083+ }
1084+
1085+ static int typealias_traverse (typealiasobject * self , visitproc visit , void * arg )
1086+ {
1087+ Py_VISIT (self -> type_params );
1088+ Py_VISIT (self -> compute_value );
1089+ Py_VISIT (self -> value );
1090+ return 0 ;
1091+ }
1092+
1093+ /*[clinic input]
1094+ typealias.__reduce__ as typealias_reduce
1095+
1096+ [clinic start generated code]*/
1097+
1098+ static PyObject *
1099+ typealias_reduce_impl (typealiasobject * self )
1100+ /*[clinic end generated code: output=913724f92ad3b39b input=4f06fbd9472ec0f1]*/
1101+ {
1102+ return PyUnicode_FromString (self -> name );
1103+ }
1104+
1105+ static PyMethodDef typealias_methods [] = {
1106+ TYPEALIAS_REDUCE_METHODDEF
1107+ {0 }
1108+ };
1109+
1110+ PyDoc_STRVAR (typealias_doc ,
1111+ "Type alias.\n\
1112+ \n\
1113+ Type aliases are created through the type statement:\n\
1114+ \n\
1115+ type Alias = int\n\
1116+ " );
1117+
1118+ PyTypeObject _PyTypeAlias_Type = {
1119+ PyVarObject_HEAD_INIT (& PyType_Type , 0 )
1120+ .tp_name = "TypeAlias" ,
1121+ .tp_basicsize = sizeof (typealiasobject ),
1122+ .tp_dealloc = typealias_dealloc ,
1123+ .tp_repr = typealias_repr ,
1124+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ,
1125+ .tp_members = typealias_members ,
1126+ .tp_methods = typealias_methods ,
1127+ .tp_doc = typealias_doc ,
1128+ .tp_traverse = typealias_traverse ,
1129+ };
1130+
1131+ PyObject *
1132+ _Py_make_typealias (PyThreadState * unused , PyObject * args )
1133+ {
1134+ assert (PyTuple_Check (args ));
1135+ assert (PyTuple_GET_SIZE (args ) == 3 );
1136+ PyObject * name = PyTuple_GET_ITEM (args , 0 );
1137+ assert (PyUnicode_Check (name ));
1138+ const char * name_str = PyUnicode_AsUTF8 (name );
1139+ if (name_str == NULL ) {
1140+ return NULL ;
1141+ }
1142+ PyObject * type_params = PyTuple_GET_ITEM (args , 1 );
1143+ PyObject * compute_value = PyTuple_GET_ITEM (args , 2 );
1144+ return (PyObject * )typealias_alloc (name_str , type_params , compute_value );
1145+ }
1146+
10331147PyDoc_STRVAR (generic_doc ,
10341148"Abstract base class for generic types.\n\
10351149\n\
0 commit comments