How to automatically ensure a requirement on a user-defined Python toolchain #2061
Unanswered
nicholasjng
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I'm working on a package (https://github.com/nicholasjng/nanobind-bazel) containing rules to produce C++ bindings extensions for Python projects using nanobind. Recently, nanobind gained the functionality for creating stub files from a built extension using a Python script, which is packaged within the repository.
I want to implement a
nanobind_stubgen()
rule that takes an already built extension, and optionally all of the other supported arguments, runs the stubgen Python script with these arguments, and outputs the generated stub file into the build tree. However, what happens in the stubgen script is highly Python-version-specific:void foo(std::optional<int>)
would be type-hinted asdef foo(x: typing.Optional[int]) -> None
, while for Python 3.10 it would bedef foo(x: int | None) -> None
. So the Python runtime used to invoke the stubgen script needs to match the Python toolchain for which the bindings were built.typing_extensions
package installed for Python < 3.11. As a requirement marker, this would betyping_extensions; python_version < '3.11'
.My question is: If I, as library author, would like to expose the stubgen script as a rule, how can I ensure that
a) the current user-configured toolchain is used (is, and b), that the used toolchain has//python:current_py_toolchain
enough?)typing_extensions
installed if its version is less than 3.11? Also, does this work with apy_binary
, or does agenrule
make more sense here?I would like to avoid defining my own toolchains within nanobind-bazel, since it would be redundant with the toolchains defined by the user. Thanks for any hints on this!
EDIT: I experimented a bit, and found that
py_binary
is invoked with the currently configured toolchain, so point a) is clear.Beta Was this translation helpful? Give feedback.
All reactions