Repository navigation
Conversation
Opt-in experimental-inspect feature: Element::DTYPE_HINT names the numpy scalar type, PyArray's TYPE_HINT and PyReadonlyArray/PyReadwriteArray's INPUT_TYPE are numpy.typing.NDArray[numpy.<scalar>], and PyUntypedArray, PyArrayDescr and PyBitGenerator get their numpy class as TYPE_HINT.
# Conflicts: # CHANGELOG.md
|
@Icxolu I resolved the changelog conflict with PyO3/rust-numpy#578. would you have time to review this? happy to change anything |
|
Thank you for the PR and sorry that i have not gotten around to this yet. In general I'm open to add this behind an experimental feature flag. I will try to give this a review next weekend. But I also have to say that I'm not super familiar with the current state of typing in PyO3, so I may have to look into that a bit at the same time. |
numpy 1 on 32-bit Windows has distinct 32-bit scalar types: numpy.intp is intc while isize maps to NPY_LONG (int_), so a type identity check fails although the hint names an equivalent dtype.
Icxolu
left a comment
There was a problem hiding this comment.
Overall I think this looks reasonable. It looks like PyArrayLike is still missing a hint, maybe numpy.typing.ArrayLike would work, or is there a specific reason you skipped it?
I think currently all the ci runs will enable this experimental feature. I'd like to see at least some runs that don't use it, so that we can be sure that we keep compiling as normal without it.
| #[cfg(feature = "experimental-inspect")] | ||
| const TYPE_HINT: pyo3::inspect::PyStaticExpr = pyo3::type_hint_subscript!( | ||
| pyo3::type_hint_identifier!("numpy.typing", "NDArray"), | ||
| T::DTYPE_HINT | ||
| ); | ||
|
|
There was a problem hiding this comment.
Not really for this PR, but we might be able to also encode the correct dimension of the array in the type hint here.
|
Btw there was a request for PyO3 to stabilise these constants so that downstream crates don't need to have the feature gate, maybe we should do that and avoid rust-numpy having to add feature combinations to CI? |
|
I think that would be quite nice for libraries, yes. If we consider that in pyo3 I would suggest to leave this open util the next pyo3 release to avoid adding a feature now which we have to remove then again. |
With PyO3's
experimental-inspectstub generation, array arguments and returns currently come out as_typeshed.Incomplete. This adds an opt-inexperimental-inspectfeature:Element::DTYPE_HINT: the NumPy scalar type.impl_element_scalar!now takes the name (e.g.f64 => NPY_DOUBLE, "float64"). It defaults totyping.Any, so external impls still compile.PyArray<T, D>::TYPE_HINTandPyReadonlyArray/PyReadwriteArray::INPUT_TYPEgivenumpy.typing.NDArray[numpy.<scalar>].PyUntypedArray,PyArrayDescrandPyBitGeneratorhintnumpy.ndarray,numpy.dtypeandnumpy.random.BitGenerator.test_type_hintschecks that each hint resolves to the actual type object in the installed NumPy. It passes on 2.5 and 1.26, and the lib builds on Rust 1.85. Datetime, string andbf16elements staytyping.Any.Written with AI assistance.