Skip to content

Commit 958c114

Browse files
committed
Prepend all function signatures to docstrings
1 parent ace4deb commit 958c114

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

docs/advanced/misc.rst

+31
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,37 @@ Note that changes to the settings affect only function bindings created during t
302302
lifetime of the ``options`` instance. When it goes out of scope at the end of the module's init function,
303303
the default settings are restored to prevent unwanted side effects.
304304

305+
Overloaded functions
306+
--------------------
307+
308+
For overloaded functions, all function overload signatures are prepended
309+
to the docstring of a function,
310+
and all docstrings are concatenated together into sections
311+
that are separated by their function signature.
312+
313+
For example:
314+
315+
.. code-block:: pycon
316+
317+
>>> help(example.add)
318+
319+
add(...)
320+
| add(arg0: int, arg1: int) -> int \
321+
| add(arg0: float, arg1: float) -> float
322+
| Overloaded function.
323+
|
324+
| 1. add(arg0: int, arg1: int) -> int
325+
|
326+
| Add two integers together.
327+
|
328+
| 2. add(arg0: float, arg1: float) -> float
329+
|
330+
| Add two floating points numbers together.
331+
332+
Calling ``options.disable_function_signatures()``, as shown previously,
333+
will cause docstrings to be generated without the prepended function signatures
334+
and without the section headings.
335+
305336
.. [#f4] http://www.sphinx-doc.org
306337
.. [#f5] http://github.com/pybind/python_example
307338

include/pybind11/pybind11.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,12 @@ class cpp_function : public function {
408408
/* Create a nice pydoc rec including all signatures and
409409
docstrings of the functions in the overload chain */
410410
if (chain && options::show_function_signatures()) {
411-
// First a generic signature
412-
signatures += rec->name;
413-
signatures += "(*args, **kwargs)\n";
411+
for (auto it = chain_start; it != nullptr; it = it->next) {
412+
signatures += rec->name;
413+
signatures += it->signature;
414+
if (it->next != nullptr) signatures += " \\";
415+
signatures += "\n";
416+
}
414417
signatures += "Overloaded function.\n\n";
415418
}
416419
// Then specific overload signatures

tests/test_factory_constructors.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ def test_init_factory_signature(msg):
8888
assert (
8989
msg(m.TestFactory1.__init__.__doc__)
9090
== """
91-
__init__(*args, **kwargs)
91+
__init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) -> None \\
92+
__init__(self: m.factory_constructors.TestFactory1, arg0: str) -> None \\
93+
__init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None \\
94+
__init__(self: m.factory_constructors.TestFactory1, arg0: handle, arg1: int, arg2: handle) -> None
9295
Overloaded function.
9396
9497
1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) -> None

0 commit comments

Comments
 (0)