Skip to content

Commit

Permalink
DOC: Ensure that we add documentation also as to the dict for types
Browse files Browse the repository at this point in the history
This ensures that `help(np.dtype)` produces a result.  I am not
exactly sure why it picks up `__doc__` from the dict instead of
`tp_doc` right now. It probably is due to the combination of
inheritance and the fact that the dict always includes `None`
and gets preference during inheritance.
(That probably makes a lot of sense to not inherit the `type`
docstring by default.)

Modifying the dictionary directly is not really good style, either,
but hopefully works.

Closes numpygh-18740
  • Loading branch information
seberg committed Apr 14, 2021
1 parent 4c780c2 commit abf8ffd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion numpy/core/src/multiarray/compiled_base.c
Expand Up @@ -1425,9 +1425,23 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *args)
PyCFunctionObject *new = (PyCFunctionObject *)obj;
_ADDDOC(new->m_ml->ml_doc, new->m_ml->ml_name);
}
else if (Py_TYPE(obj) == &PyType_Type) {
else if (PyObject_TypeCheck(obj, &PyType_Type)) {
/*
* We add it to both `tp_doc` and `__doc__` here. Note that in theory
* `tp_doc` extracts the signature line, but we currently do not use
* it. It may make sense to only add it as `__doc__` and
* `__text_signature__` to the dict in the future.
* The dictionary path is only necessary for heaptypes (currently not
* used) and metaclasses.
*/
PyTypeObject *new = (PyTypeObject *)obj;
_ADDDOC(new->tp_doc, new->tp_name);
if (new->tp_dict != NULL && PyDict_CheckExact(new->tp_dict)) {
/* Warning: Modifying `tp_dict` is not generally safe! */
if (PyDict_SetItemString(new->tp_dict, "__doc__", str) < 0) {
return NULL;
}
}
}
else if (Py_TYPE(obj) == &PyMemberDescr_Type) {
PyMemberDescrObject *new = (PyMemberDescrObject *)obj;
Expand Down

0 comments on commit abf8ffd

Please sign in to comment.