From a3a3eb27414129223c8401c86b08025c9d4482d2 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Mon, 12 Aug 2024 10:52:33 -0400 Subject: [PATCH 1/3] float | int should ser ints as ints --- src/serializers/type_serializers/float.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/serializers/type_serializers/float.rs b/src/serializers/type_serializers/float.rs index 13af9294c..4e0b2a808 100644 --- a/src/serializers/type_serializers/float.rs +++ b/src/serializers/type_serializers/float.rs @@ -12,7 +12,7 @@ use crate::tools::SchemaDict; use super::simple::to_str_json_key; use super::{ infer_json_key, infer_serialize, infer_to_python, BuildSerializer, CombinedSerializer, Extra, IsType, ObType, - SerMode, TypeSerializer, + SerCheck, SerMode, TypeSerializer, }; #[derive(Debug, Clone)] @@ -73,12 +73,18 @@ impl TypeSerializer for FloatSerializer { let py = value.py(); match extra.ob_type_lookup.is_type(value, ObType::Float) { IsType::Exact => Ok(value.into_py(py)), - IsType::Subclass => match extra.mode { - SerMode::Json => { - let rust_value = value.extract::()?; - Ok(rust_value.to_object(py)) + IsType::Subclass => match extra.check { + SerCheck::Strict => { + extra.warnings.on_fallback_py(self.get_name(), value, extra)?; + infer_to_python(value, include, exclude, extra) } - _ => infer_to_python(value, include, exclude, extra), + SerCheck::Lax | SerCheck::None => match extra.mode { + SerMode::Json => { + let rust_value = value.extract::()?; + Ok(rust_value.to_object(py)) + } + _ => infer_to_python(value, include, exclude, extra), + }, }, IsType::False => { extra.warnings.on_fallback_py(self.get_name(), value, extra)?; From ed05d795ef8979f15c181a1391bb8d3ce8ef057f Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Mon, 12 Aug 2024 10:55:24 -0400 Subject: [PATCH 2/3] adding ser test --- tests/serializers/test_union.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/serializers/test_union.py b/tests/serializers/test_union.py index 86811e560..44613b674 100644 --- a/tests/serializers/test_union.py +++ b/tests/serializers/test_union.py @@ -626,3 +626,15 @@ def test_union_serializer_picks_exact_type_over_subclass_json( ) assert s.to_python(input_value, mode='json') == expected_value assert s.to_json(input_value) == json.dumps(expected_value).encode() + + +def test_union_float_int() -> None: + s = SchemaSerializer(core_schema.union_schema([core_schema.float_schema(), core_schema.int_schema()])) + + assert s.to_python(1) == 1 + assert json.loads(s.to_json(1)) == 1 + + s = SchemaSerializer(core_schema.union_schema([core_schema.int_schema(), core_schema.float_schema()])) + + assert s.to_python(1) == 1 + assert json.loads(s.to_json(1)) == 1 From 8887dabd8c471945aed6b95262208053b83cdb34 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Mon, 12 Aug 2024 20:03:40 -0400 Subject: [PATCH 3/3] go straight to error --- src/serializers/type_serializers/float.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/serializers/type_serializers/float.rs b/src/serializers/type_serializers/float.rs index 4e0b2a808..132886c8c 100644 --- a/src/serializers/type_serializers/float.rs +++ b/src/serializers/type_serializers/float.rs @@ -14,6 +14,7 @@ use super::{ infer_json_key, infer_serialize, infer_to_python, BuildSerializer, CombinedSerializer, Extra, IsType, ObType, SerCheck, SerMode, TypeSerializer, }; +use crate::serializers::errors::PydanticSerializationUnexpectedValue; #[derive(Debug, Clone)] pub struct FloatSerializer { @@ -74,10 +75,7 @@ impl TypeSerializer for FloatSerializer { match extra.ob_type_lookup.is_type(value, ObType::Float) { IsType::Exact => Ok(value.into_py(py)), IsType::Subclass => match extra.check { - SerCheck::Strict => { - extra.warnings.on_fallback_py(self.get_name(), value, extra)?; - infer_to_python(value, include, exclude, extra) - } + SerCheck::Strict => Err(PydanticSerializationUnexpectedValue::new_err(None)), SerCheck::Lax | SerCheck::None => match extra.mode { SerMode::Json => { let rust_value = value.extract::()?;