Skip to content

Commit 4c36fb7

Browse files
authored
[DOC] avoid C++ types in docstrings (#2441)
* doc: avoid C++ types in docstrings * A bit of rewording * Another bit of rewording * Third rewording
1 parent 3a89bff commit 4c36fb7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/advanced/misc.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,34 @@ the default settings are restored to prevent unwanted side effects.
304304

305305
.. [#f4] http://www.sphinx-doc.org
306306
.. [#f5] http://github.com/pybind/python_example
307+
308+
.. _avoiding-cpp-types-in-docstrings:
309+
310+
Avoiding C++ types in docstrings
311+
================================
312+
313+
Docstrings are generated at the time of the declaration, e.g. when ``.def(...)`` is called.
314+
At this point parameter and return types should be known to pybind11.
315+
If a custom type is not exposed yet through a ``py::class_`` constructor or a custom type caster,
316+
its C++ type name will be used instead to generate the signature in the docstring:
317+
318+
.. code-block:: text
319+
320+
| __init__(...)
321+
| __init__(self: example.Foo, arg0: ns::Bar) -> None
322+
^^^^^^^
323+
324+
325+
This limitation can be circumvented by ensuring that C++ classes are registered with pybind11
326+
before they are used as a parameter or return type of a function:
327+
328+
.. code-block:: cpp
329+
330+
PYBIND11_MODULE(example, m) {
331+
332+
auto pyFoo = py::class_<ns::Foo>(m, "Foo");
333+
auto pyBar = py::class_<ns::Bar>(m, "Bar");
334+
335+
pyFoo.def(py::init<const ns::Bar&>());
336+
pyBar.def(py::init<const ns::Foo&>());
337+
}

0 commit comments

Comments
 (0)