@@ -34,22 +34,23 @@ pub fn py_to_yaml_value(obj: &Bound<'_, PyAny>) -> PyResult<Value> {
3434 if f. is_finite ( ) {
3535 Ok ( Value :: Number ( serde_yaml:: Number :: from ( f) ) )
3636 } else {
37- Err ( PyErr :: new :: < pyo3:: exceptions:: PyValueError , _ > (
38- format ! ( "Cannot convert float value {f} to YAML number" ) ,
39- ) )
37+ Err ( PyErr :: new :: < pyo3:: exceptions:: PyValueError , _ > ( format ! (
38+ "Cannot convert float value {f} to YAML number"
39+ ) ) )
4040 }
4141 } else if obj. is_instance_of :: < PyString > ( ) {
4242 Ok ( Value :: String ( obj. extract :: < String > ( ) ?) )
4343 } else if obj. is_instance_of :: < PyList > ( ) {
44- let list = obj. downcast :: < PyList > ( ) ?;
44+ let list = obj. cast :: < PyList > ( ) ?;
4545 let items: PyResult < Vec < Value > > = list. iter ( ) . map ( |item| py_to_yaml_value ( & item) ) . collect ( ) ;
4646 Ok ( Value :: Sequence ( items?) )
4747 } else if obj. is_instance_of :: < PyTuple > ( ) {
48- let tuple = obj. downcast :: < PyTuple > ( ) ?;
49- let items: PyResult < Vec < Value > > = tuple. iter ( ) . map ( |item| py_to_yaml_value ( & item) ) . collect ( ) ;
48+ let tuple = obj. cast :: < PyTuple > ( ) ?;
49+ let items: PyResult < Vec < Value > > =
50+ tuple. iter ( ) . map ( |item| py_to_yaml_value ( & item) ) . collect ( ) ;
5051 Ok ( Value :: Sequence ( items?) )
5152 } else if obj. is_instance_of :: < PyDict > ( ) {
52- let dict = obj. downcast :: < PyDict > ( ) ?;
53+ let dict = obj. cast :: < PyDict > ( ) ?;
5354 let mut mapping = serde_yaml:: Mapping :: new ( ) ;
5455 for ( k, v) in dict. iter ( ) {
5556 let key = py_to_yaml_value ( & k) ?;
@@ -71,7 +72,7 @@ pub fn py_to_yaml_value(obj: &Bound<'_, PyAny>) -> PyResult<Value> {
7172/// This function recurses without a depth limit. The input comes from
7273/// `serde_yaml::from_str`, which enforces its own recursion limit (128
7374/// by default), so the nesting depth is bounded in practice.
74- pub fn yaml_value_to_py ( py : Python < ' _ > , value : & Value ) -> PyResult < PyObject > {
75+ pub fn yaml_value_to_py ( py : Python < ' _ > , value : & Value ) -> PyResult < Py < PyAny > > {
7576 match value {
7677 Value :: Null => Ok ( py. None ( ) ) ,
7778 Value :: Bool ( b) => Ok ( b. into_pyobject ( py) ?. as_any ( ) . to_owned ( ) . unbind ( ) ) ,
@@ -152,7 +153,10 @@ mod tests {
152153 // i64::MAX + 1 is a valid u64 but overflows i64
153154 let large = ( i64:: MAX as u64 + 1 ) . into_pyobject ( py) . unwrap ( ) . into_any ( ) ;
154155 let val = py_to_yaml_value ( & large) . unwrap ( ) ;
155- assert_eq ! ( val, Value :: Number ( serde_yaml:: Number :: from( i64 :: MAX as u64 + 1 ) ) ) ;
156+ assert_eq ! (
157+ val,
158+ Value :: Number ( serde_yaml:: Number :: from( i64 :: MAX as u64 + 1 ) )
159+ ) ;
156160 } ) ;
157161 }
158162
@@ -192,10 +196,7 @@ mod tests {
192196 pyo3:: prepare_freethreaded_python ( ) ;
193197 Python :: with_gil ( |py| {
194198 let s = "hello" . into_pyobject ( py) . unwrap ( ) . into_any ( ) ;
195- assert_eq ! (
196- py_to_yaml_value( & s) . unwrap( ) ,
197- Value :: String ( "hello" . into( ) )
198- ) ;
199+ assert_eq ! ( py_to_yaml_value( & s) . unwrap( ) , Value :: String ( "hello" . into( ) ) ) ;
199200 } ) ;
200201 }
201202
0 commit comments