Rework of arg/return type hints to support .noconvert() #5486
+158
−125
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 tonp.typing.ArrayLike
.With
.noconvert()
however, only instances of numpy arrays are accepted, which would fitnp.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
andreturn_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 withImportError: 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 ofPath
.(See 9d0766c)
Suggested changelog entry:
Open issues:
arg_descr
/return_descr
do not work (require tracking using a Stack)typing.Literal
fail (%
,{
,}
,@
)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