How to support annotations for a custom type in a C extension?

MRAB python at mrabarnett.plus.com
Fri Sep 17 20:54:59 EDT 2021


On 2021-09-17 21:03, Marco Sulla wrote:
> I created a custom dict in a C extension. Name it `promethea`. How can
> I implement `promethea[str, str]`? Now I get:
> 
> TypeError: 'type' object is not subscriptable
> 
Somewhere you'll have a table of the class's methods. It needs an entry 
like this:


static PyMethodDef customdict_methods[] = {
...
     {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_CLASS | 
METH_O | METH_COEXIST, PyDoc_STR("See PEP 585")},
...
};


Note the flags: METH_CLASS says that it's a class method and 
METH_COEXIST says that it should use this method instead of the slot.


More information about the Python-list mailing list