pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: `def_property_readonly` does not include the function signature

Open gareth-cross opened this issue 2 years ago • 0 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

What version (or hash if on master) of pybind11 are you using?

v2.10.3

Problem description

When using def(...), the function signature is included in the docstring. When using def_property_readonly the function signature is not included in the docstring.

To make a simple example, I modified the cmake_example project, editing main.cpp to contain:

#include <pybind11/pybind11.h>

struct Foo {
    int do_something() const { return 5; }
};

namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
    py::class_<Foo>(m, "Foo")
        .def("do_something", &Foo::do_something, py::doc("Get a number"))
        .def_property_readonly("do_something_prop", &Foo::do_something, py::doc("Get a number as a property"));
}

I then compile and import the module in python:

In [1]: import cmake_example

In [2]: import inspect

In [3]: props = dict(inspect.getmembers(cmake_example.Foo))

In [4]: props['do_something'].__doc__
Out[4]: 'do_something(self: cmake_example.Foo) -> int\n\nGet a number\n'

In [5]: props['do_something_prop'].__doc__
Out[5]: 'Get a number as a property'

The expected behavior (at least according to my limited understanding) would be that do_something_prop has a docstring like do_something, including the signature with type annotations.

Is this a bug in pybind11, or a limitation of python properties?

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

gareth-cross avatar Nov 17 '23 19:11 gareth-cross