Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[smart_holder] Alternative to #2621 #5329

Draft
wants to merge 3 commits into
base: smart_holder
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/advanced/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,71 @@ Note that changes to the settings affect only function bindings created during t
lifetime of the ``options`` instance. When it goes out of scope at the end of the module's init function,
the default settings are restored to prevent unwanted side effects.

Overloaded functions
--------------------

The docstring of an overloaded function is prepended with the signature of each overload.
All overload docstrings are then concatenated together
into sections that are separated by each function signature.
The prepended signatures can be read by tools like Sphinx.

.. code-block:: cpp

PYBIND11_MODULE(example, m) {
m.def("add", [](int a, int b)->int { return a + b; },
"Add two integers together.");
m.def("add", [](float a, float b)->float { return a + b; },
"Add two floating point numbers together.");
}

The above example would produce the following docstring:

.. code-block:: pycon

>>> help(example.add)

add(...)
| Overloaded function:
|
| 1. add(arg0: int, arg1: int) -> int
|
| Add two integers together.
|
| 2. add(arg0: float, arg1: float) -> float
|
| Add two floating point numbers together.

Calling ``options.disable_function_signatures()`` as shown previously
will cause the docstrings of overloaded functions to be generated without the section headings.

.. code-block:: cpp

PYBIND11_MODULE(example, m) {
py::options options;
options.disable_function_signatures();

m.def("add", [](int a, int b)->int { return a + b; },
"A function which adds two numbers.\n"); // Note the additional newline here.
m.def("add", [](float a, float b)->float { return a + b; },
"Internally, a simple addition is performed.");
m.def("add", [](const py::none&, const py::none&)->py::none { return py::none(); },
"Both numbers can be None, and None will be returned.");
}

The above example would produce the following docstring:

.. code-block:: pycon

>>> help(example.add)
add(...)
| A function which adds two numbers.
|
| Internally, a simple addition is performed.
| Both numbers can be None, and None will be returned.

Not every overload must supply a docstring.
You may find it easier for a single overload to supply the entire docstring.

.. [#f4] http://www.sphinx-doc.org
.. [#f5] http://github.com/pybind/python_example

Expand Down
5 changes: 1 addition & 4 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,7 @@ class cpp_function : public function {
/* Create a nice pydoc rec including all signatures and
docstrings of the functions in the overload chain */
if (chain && options::show_function_signatures()) {
// First a generic signature
signatures += rec->name;
signatures += "(*args, **kwargs)\n";
signatures += "Overloaded function.\n\n";
signatures += "Overloaded function:\n\n";
}
// Then specific overload signatures
bool first_user_def = true;
Expand Down
22 changes: 22 additions & 0 deletions tests/test_docstring_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,30 @@ TEST_SUBMODULE(docstring_options, m) {
m.def("test_overloaded3", [](int) {}, py::arg("i"));
m.def("test_overloaded3", [](double) {}, py::arg("d"), "Overload docstr");

m.def(
"test_overloaded4",
[](int a, int b) -> int { return a + b; },
"A function which adds two numbers.\n");
m.def(
"test_overloaded4",
[](float a, float b) -> float { return a + b; },
"Internally, a simple addition is performed.");
m.def(
"test_overloaded4",
[](const py::none &, const py::none &) -> py::none { return py::none(); },
"Both numbers can be None, and None will be returned.");

options.enable_function_signatures();

m.def(
"test_overloaded5",
[](int a, int b) -> int { return a + b; },
"Add two integers together.");
m.def(
"test_overloaded5",
[](float a, float b) -> float { return a + b; },
"Add two floating point numbers together.");

m.def("test_function3", [](int, int) {}, py::arg("a"), py::arg("b"));
m.def("test_function4", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring");

Expand Down
19 changes: 19 additions & 0 deletions tests/test_docstring_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ def test_docstring_options():
# docstring on only second overload:
assert m.test_overloaded3.__doc__ == "Overload docstr"

# Check overload configuration behaviour matches the documentation
assert m.test_overloaded4.__doc__ == (
"A function which adds two numbers.\n\n"
"Internally, a simple addition is performed.\n"
"Both numbers can be None, and None will be returned."
)

assert m.test_overloaded5.__doc__ == (
"Overloaded function:\n"
"\n"
"1. test_overloaded5(arg0: int, arg1: int) -> int\n"
"\n"
"Add two integers together.\n"
"\n"
"2. test_overloaded5(arg0: float, arg1: float) -> float\n"
"\n"
"Add two floating point numbers together.\n"
)

# options.enable_function_signatures()
assert m.test_function3.__doc__.startswith("test_function3(a: int, b: int) -> None")

Expand Down
3 changes: 1 addition & 2 deletions tests/test_factory_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def test_init_factory_signature(msg):
assert (
msg(m.TestFactory1.__init__.__doc__)
== """
__init__(*args, **kwargs)
Overloaded function.
Overloaded function:

1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) -> None

Expand Down