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

WIP: stub generation #421

Merged
merged 21 commits into from
Feb 21, 2024
Merged

WIP: stub generation #421

merged 21 commits into from
Feb 21, 2024

Conversation

wjakob
Copy link
Owner

@wjakob wjakob commented Feb 15, 2024

This work-in-progress PR tracks progress towards a built-in stub generator. Please see issue #420 for discussion.

@wjakob wjakob force-pushed the stubgen branch 17 times, most recently from eb8c7ee to edd4425 Compare February 17, 2024 22:12
@wjakob wjakob force-pushed the stubgen branch 2 times, most recently from 9f5259c to 26b6f00 Compare February 18, 2024 07:58
@wjakob wjakob force-pushed the master branch 2 times, most recently from 2b33cb1 to 7df32f1 Compare February 18, 2024 09:37
@wjakob wjakob force-pushed the stubgen branch 2 times, most recently from 7508fc4 to 0a82920 Compare February 18, 2024 09:47
The ``__nb_signature__`` property of nanobind functions exposes:

- how many overloads a function has

- the function signature for each overload

- the docstring for each overload (or ``None``)

- the default arguments of each overload (if present), which are
  directly returned as Python objects. This leaves the complex and
  potentially ill-defined task of turning those Python objects into
  a valid Python expression to the consumer of this API.

- The returned signature references the default arguments using
  substrings of the form '\0', '\1', etc.
…or outputs

This changes the automatically generated type annotations for STL type
casters so that an operation like

```
std::vector<int> f(std::vector<int> x) { return x }
```

is documented as

```
f(Sequence[int]) -> list[int]
```

This reflects that these are quite accommodating in terms of what they
accept as inputs.

The nanobind stub generator uses this information to furthermore
generates automatic imports for the needed type annotations like
``typing.Sequence``.
@wjakob wjakob force-pushed the stubgen branch 2 times, most recently from db73871 to c95e121 Compare February 18, 2024 10:42
The old ``nb::raw_doc`` API could previously be used to override the
docstring of an entire overload chain.

This has the downside that it cannot easily be split into per-overload
signatures and docstrings that are needed by the new stub generation
tool. This commit removes ``nb::raw_doc`` and replaces it with a new
annotation ``nb::signature`` that only overrides the function type
signature of a single overload, while keeping the docstring separate.

This is an API and ABI-breaking change.
@wjakob wjakob force-pushed the stubgen branch 3 times, most recently from 76abb92 to 27ef69f Compare February 21, 2024 21:20
@wjakob wjakob merged commit 861e61a into master Feb 21, 2024
24 checks passed
@wjakob wjakob deleted the stubgen branch February 21, 2024 21:26
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.

1 participant