Skip to content

Support PyType_GetModuleName and PyType_GetFullyQualifiedName? #188

Description

@skirpichev

Those currently marked as unsupported, despite being part of the Limited API. And it looks, according to the docs, implementations should be possible. Or I miss something obvious?

PyType_GetModuleName

Return the type's module name. Equivalent to getting the :attr:type.__module__ attribute.

Possible implementation:

PyObject *
PyType_GetModuleName(PyTypeObject *type)
{
    return PyObject_GetAttrString((PyObject *)type, "__module__");
}

PyType_GetFullyQualifiedName

Return the type's fully qualified name. Equivalent to f"{type.__module__}.{type.__qualname__}", or :attr:type.__qualname__ if :attr:type.__module__ is not a string or is equal to "builtins".

(Looking on the actual code, it seems that docs are inaccurate as the alternative happens also if a string is equal to __main__.)

Possible implementation:

PyObject *
PyType_GetFullyQualifiedName(PyTypeObject *type)
{   
    PyObject *qualname = PyType_GetQualName(type);

    if (qualname == NULL) {
        return NULL;
    }

    PyObject *module = PyType_GetModuleName(type);

    if (module == NULL) {
        Py_DECREF(qualname);
        return NULL;
    }

    PyObject *result;

    if (PyUnicode_Check(module)
        && !PyUnicode_EqualToUTF8(module, "builtins")
        && !PyUnicode_EqualToUTF8(module, "__main__"))
    {   
        result = PyUnicode_FromFormat("%U.%U", module, qualname);
    }
    else {
        result = Py_NewRef(qualname);
    }
    Py_DECREF(module);
    Py_DECREF(qualname);
    return result;
}

Activity

  1. added 3 commits that reference this issue on Oct 8, 2026
    a9da0c0
    9c588a9
    f53af5c
  2. vstinner commented on Oct 8, 2026

    @vstinner
    Member

    Hi,

    Those currently marked as unsupported, despite being part of the Limited API. And it looks, according to the docs, implementations should be possible. Or I miss something obvious?

    I add functions on a case by case. Someone has to implement these new functions, write tests, update the doc. It's not free :-)

    Supporting PyType_GetModuleName() and PyType_GetFullyQualifiedName() is reasonable. I implemented it in #189 with 3 other functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions