@@ -8,11 +8,12 @@ use num_traits::{Bounded, Zero};
88#[ cfg( feature = "half" ) ]
99use pyo3:: sync:: GILOnceCell ;
1010use pyo3:: {
11+ conversion:: IntoPyObject ,
1112 exceptions:: { PyIndexError , PyValueError } ,
1213 ffi:: { self , PyTuple_Size } ,
1314 pyobject_native_type_named,
1415 types:: { PyAnyMethods , PyDict , PyDictMethods , PyTuple , PyType } ,
15- Borrowed , Bound , Py , PyAny , PyObject , PyResult , PyTypeInfo , Python , ToPyObject ,
16+ Borrowed , Bound , Py , PyAny , PyObject , PyResult , PyTypeInfo , Python ,
1617} ;
1718
1819use crate :: npyffi:: {
@@ -80,8 +81,14 @@ impl PyArrayDescr {
8081 ///
8182 /// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html
8283 #[ inline]
83- pub fn new < ' py , T : ToPyObject + ?Sized > ( py : Python < ' py > , ob : & T ) -> PyResult < Bound < ' py , Self > > {
84- fn inner ( py : Python < ' _ > , obj : PyObject ) -> PyResult < Bound < ' _ , PyArrayDescr > > {
84+ pub fn new < ' a , ' py , T > ( py : Python < ' py > , ob : T ) -> PyResult < Bound < ' py , Self > >
85+ where
86+ T : IntoPyObject < ' py > ,
87+ {
88+ fn inner < ' py > (
89+ py : Python < ' py > ,
90+ obj : Borrowed < ' _ , ' py , PyAny > ,
91+ ) -> PyResult < Bound < ' py , PyArrayDescr > > {
8592 let mut descr: * mut PyArray_Descr = ptr:: null_mut ( ) ;
8693 unsafe {
8794 // None is an invalid input here and is not converted to NPY_DEFAULT_TYPE
@@ -91,17 +98,24 @@ impl PyArrayDescr {
9198 }
9299 }
93100
94- inner ( py, ob. to_object ( py) )
101+ inner (
102+ py,
103+ ob. into_pyobject ( py)
104+ . map_err ( Into :: into) ?
105+ . into_any ( )
106+ . as_borrowed ( ) ,
107+ )
95108 }
96109
97110 /// Deprecated name for [`PyArrayDescr::new`].
98111 #[ deprecated( since = "0.23.0" , note = "renamed to `PyArrayDescr::new`" ) ]
112+ #[ allow( deprecated) ]
99113 #[ inline]
100- pub fn new_bound < ' py , T : ToPyObject + ?Sized > (
114+ pub fn new_bound < ' py , T : pyo3 :: ToPyObject + ?Sized > (
101115 py : Python < ' py > ,
102116 ob : & T ,
103117 ) -> PyResult < Bound < ' py , Self > > {
104- Self :: new ( py, ob)
118+ Self :: new ( py, ob. to_object ( py ) )
105119 }
106120
107121 /// Shortcut for creating a type descriptor of `object` type.
@@ -598,6 +612,7 @@ macro_rules! clone_methods_impl {
598612 } ;
599613}
600614pub ( crate ) use clone_methods_impl;
615+ use pyo3:: BoundObject ;
601616
602617macro_rules! impl_element_scalar {
603618 ( @impl : $ty: ty, $npy_type: expr $( , #[ $meta: meta] ) * ) => {
@@ -692,7 +707,7 @@ mod tests {
692707 assert ! ( dt. get_field( "a" ) . unwrap( ) . 0 . is( & dtype:: <PyObject >( py) ) ) ;
693708 assert ! ( dt. get_field( "b" ) . unwrap( ) . 0 . is( & dtype:: <bool >( py) ) ) ;
694709
695- assert ! ( PyArrayDescr :: new( py, & 123_usize ) . is_err( ) ) ;
710+ assert ! ( PyArrayDescr :: new( py, 123_usize ) . is_err( ) ) ;
696711 } ) ;
697712 }
698713
0 commit comments