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

stubgen: Fix generated stub filename when referencing out-of-director… #892

Merged
merged 1 commit into from
Jan 30, 2025

Conversation

nicholasjng
Copy link
Contributor

@nicholasjng nicholasjng commented Jan 29, 2025

…y extensions

Currently, to generate stubs for extensions not contained in the current directory, you have to invoke stubgen like so:

python -m nanobind.stubgen -m path.to.my.module -r -O ...

This is for example how I do it in nanobind-bazel, because Bazel invokes scripts from a hermetic "runfiles" directory, which is in general not the source directory.

In the recursive case, and possibly also the non-recursive case with an output directory specified, this leads to the creation of a file called path.to.my.pyi, which is not what we want (IDEs generally require that the stub file be of the same name as the generated module).

Hence, in that case, we infer the stub file name as the last part of the module name given to stubgen.


Before:

# NB: _main/src/ is a typical top-level directory layout of Bazel's Python runfiles.
$ python -m nanobind.stubgen -m _main.src.nanobind_example_ext -r -O src
Module "_main.src.nanobind_example_ext" ..
  - importing ..
  - analyzing ..
  - writing stub "src/sub_ext/__init__.pyi" ..
  - writing stub "src/_main.src.pyi" ..  # <- this path is wrong/nonsensical

and after:

$ python -m nanobind.stubgen -m _main.src.nanobind_example_ext -r -O src
Module "_main.src.nanobind_example_ext" ..
  - importing ..
  - analyzing ..
  - writing stub "src/sub_ext/__init__.pyi" ..
  - writing stub "src/nanobind_example_ext.pyi" ..

This is a follow-up to nicholasjng/nanobind-bazel#44, where @cemlyn007 added recursive stubgen support to nanobind-bazel. He also kindly added a workaround for this problem there, but I would love to see it fixed upstream as well.

I would appreciate your opinion - for in-tree stub generation (i.e., the extension is contained in the current directory), this does not change anything, since then, the module name will not contain any dots.

…y extensions

Currently, to generate stubs for extensions not contained in the current directory,
you have to invoke stubgen like so:

`python -m nanobind.stubgen -m path.to.my.module -r -O ...`

This is for example how I do it in nanobind-bazel, because Bazel invokes scripts
from a hermetic "runfiles" directory, which is in general not the source directory.

In the recursive case, and possibly also the non-recursive case with an output
directory specified, this leads to the creation of a file called `path.to.my.pyi`,
which is not what we want (IDEs generally require that the stub file be of the
same name as the generated module).

Hence, in that case, we infer the stub file name as the last part of the module name
given to stubgen.
@wjakob
Copy link
Owner

wjakob commented Jan 30, 2025

This looks great to me. Thanks for figuring this out!

@wjakob wjakob merged commit 185afbc into wjakob:master Jan 30, 2025
31 checks passed
@nicholasjng nicholasjng deleted the stubgen-out-of-tree-modules branch January 30, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants