Skip to content

"DeprecationWarning: builtin type has no __module__ attribute" when using PyStructSequence_NewType from a module method #124182

@ffelixg

Description

@ffelixg

Bug report

Bug description:

Below is an example producing the warning. This warning does not occur when the type is created in the module's init function.
When using PyStructSequence_InitType2, the resulting type gets "builtin" as the __module__ attribute and there is no deprecation warning.
It's also possible to add a field named __module__ to prevent the warning. Setting the __module__ attribute after creating the type does not help, the warning gets raised inside the PyStructSequence_NewType function.

I couldn't find anything indicating that I'm not supposed to use PyStructSequence_NewType in this way.

#include <Python.h>

// the type is usable, but we get: <string>:1: DeprecationWarning: builtin type TypeName has no __module__ attribute
// python3 -c "import c_ext; c_ext.create_type()"

static PyObject* create_type(PyObject* self) {
    PyStructSequence_Field fields[] = {
        { "FieldName", "docs" },
        { NULL, NULL },
    };
    PyStructSequence_Desc desc = {"TypeName", "docs", fields, 1};
    return (PyObject*)PyStructSequence_NewType(&desc);
}

static struct PyMethodDef methods[] = {
    {"create_type", (PyCFunction)create_type, METH_NOARGS},
    {NULL, NULL}
};

static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT, "c_ext", NULL, -1, methods
};

PyMODINIT_FUNC PyInit_c_ext(void) {
    return PyModule_Create(&module);
}

CPython versions tested on:

3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions