Skip to content

Commit ee182af

Browse files
committed
Prepend all function signatures to docstrings
1 parent ad6bf5c commit ee182af

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

docs/advanced/misc.rst

Lines changed: 31 additions & 0 deletions
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 to the
309+
docstring of a function, and all docstrings are concatenated together into sections
310+
that are separated by their function signature.
311+
312+
For example:
313+
314+
.. code-block:: pycon
315+
316+
>>> help(example.add)
317+
318+
add(...)
319+
| add(arg0: int, arg1: int) -> int
320+
| add(arg0: float, arg1: float) -> float
321+
|
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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,12 @@ class cpp_function : public function {
453453
/* Create a nice pydoc rec including all signatures and
454454
docstrings of the functions in the overload chain */
455455
if (chain && options::show_function_signatures()) {
456-
// First a generic signature
457-
signatures += rec->name;
458-
signatures += "(*args, **kwargs)\n";
459-
signatures += "Overloaded function.\n\n";
456+
for (auto it = chain_start; it != nullptr; it = it->next) {
457+
signatures += rec->name;
458+
signatures += it->signature;
459+
signatures += "\n";
460+
}
461+
signatures += "\nOverloaded function.\n\n";
460462
}
461463
// Then specific overload signatures
462464
bool first_user_def = true;

tests/test_factory_constructors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ 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
95+
9296
Overloaded function.
9397
9498
1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) -> None

0 commit comments

Comments
 (0)