pybind / pybind11

Seamless operability between C++11 and Python

Home Page:https://pybind11.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

gareth-cross opened this issue · comments

Required prerequisites

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