Replies: 3 comments 1 reply
-
First, I must mention that I have not generated stubs nor used any python typing tools. |
Beta Was this translation helpful? Give feedback.
-
The expressiveness of type annotations for nd-arrays is frustrating — a lot is lost in translation. I've made the change in commit 10cc510 because this was requested by many users. See #442 and #1094, #442, #983, and #984 for some starting points. This topic has been raised many times. To move to even more nuanced type annotations, I think we need a concrete proposal and a draft modification to stubgen that implements this proposal. I am not working with NumPy types in any of my projects, so it would be helpful to have a user of such annotations take the initiative. |
Beta Was this translation helpful? Give feedback.
-
I don't feel like I know NDArray type annotations well enough to write a detailed proposal. In general, I'm fine with switching to NDArray from ArrayLike and my uses would probably be fixed by switching mostly to I was going to suggest that handling TBH, I'd be completely happy to "solve" this by manually writing out the type for such args. It's just that this is a little tedious currently, as there's no way to write the type annotation of a single arg, you can only do it for the whole function More concretely, I wouldn't mind doing this (non-working example):
But right now, to override argument type hints, I'd have to do write it out completely:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I upgraded to the latest Nanobind @ 10cc510 and my
implot
stubs stubs started flagging type errors that didn’t appear before. This may not really be a regression in Nanobind but rather that VSCode Pylance was less strict about ArrayLikes than NDArrays. I'm looking for guidance on what's a good way to express NDArray inputs without introducing lot of boilerplate or unnecessary function overloads.In my
implot
bindings, I'm declaringplot_line
as below:With the older Nanobind version, this produced:
with the new version, it outputs:
Unfortunately VSCode now complains about the types if I pass a non-float64 array to
plot_line()
, e.g.(passing a non-double NDArray to this function should be completely fine, as due to the 'double' constraint, the array is converted to doubles by nanobind.)
This outputs a warning:
The xs and ys args are declared as
typedef nb::ndarray<const double, nb::ndim<1>, nb::device::cpu, nb::c_contig>
which means the input arguments gets automatically converted to a double valued array. This is exactly what I want and it works, it's just that the stub forplot_line()
doesn't reflect that. I'd like thexs
andys
args to be typed less strict as justNDArray
instead ofNDArray[np.float64]
because that captures the type of inputs accepted by this function.Below is my current attempt at declaring this in another way that produces a less strict type signature:
This produces the declaration I'm after (below), but the code doesn't actually work as the
.view()
calls do not convert the data.Any thoughts on what's a good way to express these more relaxed inputs so that the types also reflect what inputs are accepted? IMO the current generated stubs simply are too strict.
Beta Was this translation helpful? Give feedback.
All reactions