Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

34 views
Skip to first unread message

Marco Sulla

unread,
Sep 17, 2021, 4:04:40 PM9/17/21
to
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

MRAB

unread,
Sep 17, 2021, 8:58:30 PM9/17/21
to
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.

Marco Sulla

unread,
Sep 18, 2021, 2:41:06 AM9/18/21
to
Ooook..... I have a question. Why is this code not present in
dictobject.c? Where are the dict annotations implemented?
> --
> https://mail.python.org/mailman/listinfo/python-list

Serhiy Storchaka

unread,
Sep 18, 2021, 9:34:55 PM9/18/21
to
18.09.21 09:40, Marco Sulla пише:
> Ooook..... I have a question. Why is this code not present in
> dictobject.c? Where are the dict annotations implemented?

In dictobject.c.

Serhiy Storchaka

unread,
Sep 18, 2021, 9:34:55 PM9/18/21
to
18.09.21 03:54, MRAB пише:
> 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.

"(PyCFunction)" is redundant, Py_GenericAlias already has the right
type. Overuse of casting to PyCFunction can hide actual bugs.

METH_COEXIST is not needed. There is no slot for __class_getitem__, and
even if it was, there would be no reasons to prohibit using it.

MRAB

unread,
Sep 18, 2021, 10:59:58 PM9/18/21
to
On 2021-09-18 16:09, Serhiy Storchaka wrote:
> 18.09.21 03:54, MRAB пише:
>> 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.
>
> "(PyCFunction)" is redundant, Py_GenericAlias already has the right
> type. Overuse of casting to PyCFunction can hide actual bugs.
>
I borrowed that from listobject.c, which does have the cast.

MRAB

unread,
Sep 18, 2021, 11:02:11 PM9/18/21
to
I just had a look at dictobject.c. It too has a cast to PyCFunction.

Serhiy Storchaka

unread,
Sep 19, 2021, 1:18:39 PM9/19/21
to
19.09.21 05:59, MRAB пише:
> On 2021-09-18 16:09, Serhiy Storchaka wrote:
>> "(PyCFunction)" is redundant, Py_GenericAlias already has the right
>> type. Overuse of casting to PyCFunction can hide actual bugs.
>>
> I borrowed that from listobject.c, which does have the cast.

Fixed. https://github.com/python/cpython/pull/28450

Marco Sulla

unread,
Nov 18, 2021, 5:31:34 PM11/18/21
to
It works. Thanks a lot.
> --
> https://mail.python.org/mailman/listinfo/python-list
0 new messages