Mailing List Archive

How to support annotations for a custom type in a C extension?
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply to ]
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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply to ]
Ooook..... I have a question. Why is this code not present in
dictobject.c? Where are the dict annotations implemented?

On Sat, 18 Sept 2021 at 03:00, MRAB <python@mrabarnett.plus.com> wrote:
>
> 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.
> --
> https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply 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.

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply 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.

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply 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.

> 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.
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply to ]
On 2021-09-18 16:10, Serhiy Storchaka wrote:
> 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.
>
I just had a look at dictobject.c. It too has a cast to PyCFunction.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply 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

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to support annotations for a custom type in a C extension? [ In reply to ]
It works. Thanks a lot.

On Sun, 19 Sept 2021 at 19:23, Serhiy Storchaka <storchaka@gmail.com> wrote:
>
> 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
>
> --
> https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list