From 07bdd83d7d2540c77a579c1e96da0a23fcc0b592 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Thu, 21 Nov 2024 13:01:31 -0500 Subject: [PATCH 1/4] ser as complex number for Complex inference in Python --- src/serializers/infer.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/serializers/infer.rs b/src/serializers/infer.rs index bdfa74b52..cfa6bfa9d 100644 --- a/src/serializers/infer.rs +++ b/src/serializers/infer.rs @@ -232,8 +232,7 @@ pub(crate) fn infer_to_python_known( } ObType::Complex => { let v = value.downcast::()?; - let complex_str = type_serializers::complex::complex_to_str(v); - complex_str.into_py(py) + v.into_py(py) } ObType::Path => value.str()?.into_py(py), ObType::Pattern => value.getattr(intern!(py, "pattern"))?.into_py(py), @@ -285,8 +284,7 @@ pub(crate) fn infer_to_python_known( } ObType::Complex => { let v = value.downcast::()?; - let complex_str = type_serializers::complex::complex_to_str(v); - complex_str.into_py(py) + v.into_py(py) } ObType::Unknown => { if let Some(fallback) = extra.fallback { From f829bc98e165bd02f8b5bef830fb3bf1fda27918 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Thu, 21 Nov 2024 13:05:01 -0500 Subject: [PATCH 2/4] test, too --- tests/serializers/test_complex.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/serializers/test_complex.py b/tests/serializers/test_complex.py index e7c98246a..e4a3474b1 100644 --- a/tests/serializers/test_complex.py +++ b/tests/serializers/test_complex.py @@ -37,3 +37,9 @@ def test_complex_json(value, expected): assert math.isnan(c.real) else: assert c.imag == value.imag + + +def test_complex_inference() -> None: + s = SchemaSerializer(core_schema.any_schema()) + assert s.to_python(1 + 2j) == 1 + 2j + assert s.to_json(1 + 2j) == '1+2j' \ No newline at end of file From 06e37fdfadd31bc6523b11ff4a1b10331194c9a8 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Thu, 21 Nov 2024 13:18:45 -0500 Subject: [PATCH 3/4] formatting --- tests/serializers/test_complex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/serializers/test_complex.py b/tests/serializers/test_complex.py index e4a3474b1..da8f79d0c 100644 --- a/tests/serializers/test_complex.py +++ b/tests/serializers/test_complex.py @@ -42,4 +42,4 @@ def test_complex_json(value, expected): def test_complex_inference() -> None: s = SchemaSerializer(core_schema.any_schema()) assert s.to_python(1 + 2j) == 1 + 2j - assert s.to_json(1 + 2j) == '1+2j' \ No newline at end of file + assert s.to_json(1 + 2j) == '1+2j' From a51f2e93514daf31cb115c5bb86eaa6c76f8d831 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Thu, 21 Nov 2024 13:31:28 -0500 Subject: [PATCH 4/4] fix tests, ser to string for json mode --- src/serializers/infer.rs | 3 ++- tests/serializers/test_complex.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/serializers/infer.rs b/src/serializers/infer.rs index cfa6bfa9d..a188f6f60 100644 --- a/src/serializers/infer.rs +++ b/src/serializers/infer.rs @@ -232,7 +232,8 @@ pub(crate) fn infer_to_python_known( } ObType::Complex => { let v = value.downcast::()?; - v.into_py(py) + let complex_str = type_serializers::complex::complex_to_str(v); + complex_str.into_py(py) } ObType::Path => value.str()?.into_py(py), ObType::Pattern => value.getattr(intern!(py, "pattern"))?.into_py(py), diff --git a/tests/serializers/test_complex.py b/tests/serializers/test_complex.py index da8f79d0c..2805ab3bd 100644 --- a/tests/serializers/test_complex.py +++ b/tests/serializers/test_complex.py @@ -42,4 +42,4 @@ def test_complex_json(value, expected): def test_complex_inference() -> None: s = SchemaSerializer(core_schema.any_schema()) assert s.to_python(1 + 2j) == 1 + 2j - assert s.to_json(1 + 2j) == '1+2j' + assert s.to_json(1 + 2j) == b'"1+2j"'