@@ -231,11 +231,9 @@ pub(crate) fn infer_to_python_known(
231
231
PyList :: new_bound ( py, items) . into_py ( py)
232
232
}
233
233
ObType :: Complex => {
234
- let dict = value. downcast :: < PyDict > ( ) ?;
235
- let new_dict = PyDict :: new_bound ( py) ;
236
- let _ = new_dict. set_item ( "real" , dict. get_item ( "real" ) ?) ;
237
- let _ = new_dict. set_item ( "imag" , dict. get_item ( "imag" ) ?) ;
238
- new_dict. into_py ( py)
234
+ let v = value. downcast :: < PyComplex > ( ) ?;
235
+ let complex_str = type_serializers:: complex:: complex_to_str ( v) ;
236
+ complex_str. into_py ( py)
239
237
}
240
238
ObType :: Path => value. str ( ) ?. into_py ( py) ,
241
239
ObType :: Pattern => value. getattr ( intern ! ( py, "pattern" ) ) ?. into_py ( py) ,
@@ -286,11 +284,9 @@ pub(crate) fn infer_to_python_known(
286
284
iter. into_py ( py)
287
285
}
288
286
ObType :: Complex => {
289
- let dict = value. downcast :: < PyDict > ( ) ?;
290
- let new_dict = PyDict :: new_bound ( py) ;
291
- let _ = new_dict. set_item ( "real" , dict. get_item ( "real" ) ?) ;
292
- let _ = new_dict. set_item ( "imag" , dict. get_item ( "imag" ) ?) ;
293
- new_dict. into_py ( py)
287
+ let v = value. downcast :: < PyComplex > ( ) ?;
288
+ let complex_str = type_serializers:: complex:: complex_to_str ( v) ;
289
+ complex_str. into_py ( py)
294
290
}
295
291
ObType :: Unknown => {
296
292
if let Some ( fallback) = extra. fallback {
@@ -422,10 +418,8 @@ pub(crate) fn infer_serialize_known<S: Serializer>(
422
418
ObType :: Bool => serialize ! ( bool ) ,
423
419
ObType :: Complex => {
424
420
let v = value. downcast :: < PyComplex > ( ) . map_err ( py_err_se_err) ?;
425
- let mut map = serializer. serialize_map ( Some ( 2 ) ) ?;
426
- map. serialize_entry ( & "real" , & v. real ( ) ) ?;
427
- map. serialize_entry ( & "imag" , & v. imag ( ) ) ?;
428
- map. end ( )
421
+ let complex_str = type_serializers:: complex:: complex_to_str ( v) ;
422
+ Ok ( serializer. collect_str :: < String > ( & complex_str) ?)
429
423
}
430
424
ObType :: Float | ObType :: FloatSubclass => {
431
425
let v = value. extract :: < f64 > ( ) . map_err ( py_err_se_err) ?;
@@ -672,7 +666,7 @@ pub(crate) fn infer_json_key_known<'a>(
672
666
}
673
667
Ok ( Cow :: Owned ( key_build. finish ( ) ) )
674
668
}
675
- ObType :: List | ObType :: Set | ObType :: Frozenset | ObType :: Dict | ObType :: Generator | ObType :: Complex => {
669
+ ObType :: List | ObType :: Set | ObType :: Frozenset | ObType :: Dict | ObType :: Generator => {
676
670
py_err ! ( PyTypeError ; "`{}` not valid as object key" , ob_type)
677
671
}
678
672
ObType :: Dataclass | ObType :: PydanticSerializable => {
@@ -689,6 +683,10 @@ pub(crate) fn infer_json_key_known<'a>(
689
683
// FIXME it would be nice to have a "PyCow" which carries ownership of the Python type too
690
684
Ok ( Cow :: Owned ( key. str ( ) ?. to_string_lossy ( ) . into_owned ( ) ) )
691
685
}
686
+ ObType :: Complex => {
687
+ let v = key. downcast :: < PyComplex > ( ) ?;
688
+ Ok ( type_serializers:: complex:: complex_to_str ( v) . into ( ) )
689
+ }
692
690
ObType :: Pattern => Ok ( Cow :: Owned (
693
691
key. getattr ( intern ! ( key. py( ) , "pattern" ) ) ?
694
692
. str ( ) ?
0 commit comments