@@ -257,7 +257,7 @@ static PyArray_Descr *
257
257
_convert_from_tuple (PyObject * obj , int align )
258
258
{
259
259
if (PyTuple_GET_SIZE (obj ) != 2 ) {
260
- PyErr_Format (PyExc_TypeError ,
260
+ PyErr_Format (PyExc_TypeError ,
261
261
"Tuple must have size 2, but has size %zd" ,
262
262
PyTuple_GET_SIZE (obj ));
263
263
return NULL ;
@@ -449,8 +449,8 @@ _convert_from_array_descr(PyObject *obj, int align)
449
449
for (int i = 0 ; i < n ; i ++ ) {
450
450
PyObject * item = PyList_GET_ITEM (obj , i );
451
451
if (!PyTuple_Check (item ) || (PyTuple_GET_SIZE (item ) < 2 )) {
452
- PyErr_Format (PyExc_TypeError ,
453
- "Field elements must be 2- or 3-tuples, got '%R'" ,
452
+ PyErr_Format (PyExc_TypeError ,
453
+ "Field elements must be 2- or 3-tuples, got '%R'" ,
454
454
item );
455
455
goto fail ;
456
456
}
@@ -461,7 +461,7 @@ _convert_from_array_descr(PyObject *obj, int align)
461
461
}
462
462
else if (PyTuple_Check (name )) {
463
463
if (PyTuple_GET_SIZE (name ) != 2 ) {
464
- PyErr_Format (PyExc_TypeError ,
464
+ PyErr_Format (PyExc_TypeError ,
465
465
"If a tuple, the first element of a field tuple must have "
466
466
"two elements, not %zd" ,
467
467
PyTuple_GET_SIZE (name ));
@@ -475,7 +475,7 @@ _convert_from_array_descr(PyObject *obj, int align)
475
475
}
476
476
}
477
477
else {
478
- PyErr_SetString (PyExc_TypeError ,
478
+ PyErr_SetString (PyExc_TypeError ,
479
479
"First element of field tuple is "
480
480
"neither a tuple nor str" );
481
481
goto fail ;
@@ -3101,6 +3101,30 @@ arraydescr_newbyteorder(PyArray_Descr *self, PyObject *args)
3101
3101
return (PyObject * )PyArray_DescrNewByteorder (self , endian );
3102
3102
}
3103
3103
3104
+ static PyObject *
3105
+ arraydescr_class_getitem (PyObject * cls , PyObject * args )
3106
+ {
3107
+ PyObject * generic_alias ;
3108
+
3109
+ #ifdef Py_GENERICALIASOBJECT_H
3110
+ Py_ssize_t args_len ;
3111
+
3112
+ args_len = PyTuple_Check (args ) ? PyTuple_Size (args ) : 1 ;
3113
+ if (args_len != 1 ) {
3114
+ return PyErr_Format (PyExc_TypeError ,
3115
+ "Too %s arguments for %s" ,
3116
+ args_len > 1 ? "many" : "few" ,
3117
+ ((PyTypeObject * )cls )-> tp_name );
3118
+ }
3119
+ generic_alias = Py_GenericAlias (cls , args );
3120
+ #else
3121
+ PyErr_SetString (PyExc_TypeError ,
3122
+ "Type subscription requires python >= 3.9" );
3123
+ generic_alias = NULL ;
3124
+ #endif
3125
+ return generic_alias ;
3126
+ }
3127
+
3104
3128
static PyMethodDef arraydescr_methods [] = {
3105
3129
/* for pickling */
3106
3130
{"__reduce__" ,
@@ -3112,6 +3136,10 @@ static PyMethodDef arraydescr_methods[] = {
3112
3136
{"newbyteorder" ,
3113
3137
(PyCFunction )arraydescr_newbyteorder ,
3114
3138
METH_VARARGS , NULL },
3139
+ /* for typing; requires python >= 3.9 */
3140
+ {"__class_getitem__" ,
3141
+ (PyCFunction )arraydescr_class_getitem ,
3142
+ METH_CLASS | METH_O , NULL },
3115
3143
{NULL , NULL , 0 , NULL } /* sentinel */
3116
3144
};
3117
3145
0 commit comments