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

Rework of arg/return type hints to support .noconvert() #5486

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

timohl
Copy link
Contributor

@timohl timohl commented Jan 6, 2025

Description

While working on improving type hints for Eigen/numpy, I found that with the current solution from #5450 it is not possible to have the type hints be influenced by the py::arg().noconvert() option.
Certain Eigen types offer to be loaded from anything np.array() accepts which would fit to np.typing.ArrayLike.
With .noconvert() however, only instances of numpy arrays are accepted, which would fit np.typing.NDArray[<dtype>].
It would be nice if .noconvert() would force using the return type (which is the more specific type).

Meanwhile, I found that nanobind implements a similar arg/return type hint system using io_name.
This system inserts @arg-type@return-type@ using @ as a special character, which is evaluated in a later signature parsing stage.
At this stage, the .noconvert() info is available, and I was able to implement it similarly.

Additionally, I added arg_descr and return_descr to force using arg or return type hints.
This is useful for correctly type hinting typing::Callable.
The current implementation in this PR does not allow nested Callables (e.g., Callable that has a Callable as argument), but I will add support for that later this week (requires using a stack container to track nested arg_descr/return_descr).
(See 077d0b6)

Finally, I found bugs when using typing::Literal with certain special characters:
Currently, %, {, } fail with ImportError: Internal error while parsing type signature (2).
With this PR, the added special character @ fails as well when used in a Literal.
I will try to find a solution for that later this week as well.
(See 9877aca)

This also fixes #5477 by using pathlib.Path instead of Path.
(See 9d0766c)

Suggested changelog entry:

Rework of arg/return type hints to support .noconvert()

Open issues:

  • Nested arg_descr/return_descr do not work (require tracking using a Stack)
  • Special characters in typing.Literal fail (%, {, }, @)
  • Fix usage of auto return type deduction (not supported in C++11)
  • Fix clang-tidy issues

Relevant nanobind code

This PR is based on the following code in nanobind:

https://github.com/wjakob/nanobind/blob/698f449a207e321b364278ff68f0b161127fd3a0/include/nanobind/nb_descr.h#L81-L86

https://github.com/wjakob/nanobind/blob/698f449a207e321b364278ff68f0b161127fd3a0/src/nb_func.cpp#L1018-L1234

@timohl
Copy link
Contributor Author

timohl commented Jan 6, 2025

Checks using C++11 fail because I used auto return type deduction which is only supported since C++14.
Will fix that soon.

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.

[BUG]: Wrong type annotation when using std::filesystem::path as a return type, causes stubgen to choke
1 participant