[PyQt] QtProperty docstrings

Javier Santacruz López-Cepero jsl at taric.es
Thu Oct 8 13:10:19 BST 2015


Hello to all,

I think I detected a strange behaviour with docstrings when using the
pyqtProperty decorator.
It does get the docstring from the doc pyqtProperty parameter but not from
the docstring itself:

    from PyQt5 import QtCore, QtWidgets

    class Test(QtWidgets.QWidget):
        @pyqtProperty(str)
        def data(self):
            """docstring"""
            return "result"

When defining a property like this, the __doc__ attribute is not
initialized:


    In [11]: print(Test.data.__doc__)
    None

But it does work when set directly from the doc parameter:


    from PyQt5 import QtCore, QtWidgets

    class Test(QtWidgets.QWidget):
        @pyqtProperty(str, doc="docstring")
        def data(self):
            return "result"

    In [13]: print(Test.data.__doc__)
    docstring

I've searched through the code and found this fragment in the file
"qpy/QtCore/qpycore_pyqtproperty.cpp" that looks like it should be getting
the docstrings from the wrapped function docstring:

    /* If no docstring was given and the getter has one, then use it. */
    if ((!doc || doc == Py_None) && get)
    {
        PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");

        if (get_doc)
        {
            Py_XDECREF(doc);
            doc = get_doc;
        }
        else
        {
            PyErr_Clear();
        }
    }


Thank you :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20151008/d63aebe6/attachment.html>


More information about the PyQt mailing list