Skip to content

Commit c360ceb

Browse files
authored
docs: Add stubgen instructions using Meson (#1109)
1 parent 0a059e1 commit c360ceb

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

docs/meson.rst

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ The ``meson.build`` definition in your project root should look like:
5757
default_options: ['cpp_std=c++17', 'b_ndebug=if-release'],
5858
)
5959
60-
py = import('python').find_installation()
60+
python = import('python').find_installation()
6161
nanobind_dep = dependency('nanobind')
62-
py.extension_module(
62+
mod = python.extension_module(
6363
'my_module_name',
6464
sources: ['path_to_module.cpp'],
6565
dependencies: [nanobind_dep],
@@ -103,9 +103,9 @@ to build extensions against the CPython 3.12 stable ABI, use:
103103
default_options: ['cpp_std=c++17', 'b_ndebug=if-release'],
104104
)
105105
106-
py = import('python').find_installation()
106+
python = import('python').find_installation()
107107
nanobind_dep = dependency('nanobind')
108-
py.extension_module(
108+
mod = python.extension_module(
109109
'my_module_name',
110110
sources: ['path_to_module.cpp'],
111111
dependencies: [nanobind_dep],
@@ -114,3 +114,22 @@ to build extensions against the CPython 3.12 stable ABI, use:
114114
)
115115
116116
as your ``meson.build`` file.
117+
118+
Stub generation
119+
---------------
120+
121+
You can configure the build to write a stub file for your extension module
122+
by adding the following to ``meson.build``:
123+
124+
.. code-block:: meson
125+
126+
stubgen = nanobind_dep.get_variable('stubgen')
127+
custom_target(
128+
output: 'my_module_name.pyi',
129+
depends: mod,
130+
command: [python, stubgen, '-m', 'my_module_name', '-M', 'py.typed'],
131+
build_by_default: true
132+
)
133+
134+
Then, after building your module, the build system will use nanobind's command
135+
line interface for :ref:`stub generation <stubs>`.

docs/typing.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,14 @@ An undocumented stub replaces the entire body with the Python ellipsis object
357357
def square(x: int) -> int: ...
358358
359359
Complex default arguments are often also abbreviated with ``...`` to improve
360-
the readability of signatures. You can read more about stub files in the
361-
`typing documentation
362-
<https://typing.readthedocs.io/en/latest/source/stubs.html>`__ and the `MyPy
363-
documentation <https://mypy.readthedocs.io/en/stable/stubs.html>`__.
360+
the readability of signatures.
361+
362+
You can read more about stub files in
363+
`Writing and Maintaining Stub Files
364+
<https://typing.python.org/en/latest/guides/writing_stubs.html>`__ and
365+
`Distributing type information
366+
<https://typing.python.org/en/latest/spec/distributing.html>`__ and in the
367+
`MyPy documentation <https://mypy.readthedocs.io/en/stable/stubs.html>`__.
364368

365369
nanobind's ``stubgen`` tool automates the process of stub generation to turn
366370
modules containing a mixture of ordinary Python code and C++ bindings into an

0 commit comments

Comments
 (0)