pybind / pybind/pybind11

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

Open
#4,943 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the modified cmake_example/main.cpp example and compare the doc values for def() and def_property_readonly() using Python's inspect.getmembers. Trace the pybind11 binding entry point for def_property_readonly and its generated documentation, then add coverage for the expected signature-bearing docstring and verify the property output matches the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.