Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/serializers/type_serializers/datetime_etc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ macro_rules! build_temporal_serializer {
exclude: Option<&Bound<'_, PyAny>>,
extra: &Extra,
) -> PyResult<Py<PyAny>> {
match extra.mode {
SerMode::Json => match $downcast(value) {
Ok(py_value) => Ok(self.temporal_mode.$to_json(value.py(), py_value)?),
Err(_) => {
extra.warnings.on_fallback_py(self.get_name(), value, extra)?;
infer_to_python(value, include, exclude, extra)
}
match $downcast(value) {
Ok(py_value) => match extra.mode {
SerMode::Json => Ok(self.temporal_mode.$to_json(value.py(), py_value)?),
_ => Ok(value.clone().unbind()),
},
_ => infer_to_python(value, include, exclude, extra),
_ => {
extra.warnings.on_fallback_py(self.get_name(), value, extra)?;
infer_to_python(value, include, exclude, extra)
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/serializers/type_serializers/timedelta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ impl TypeSerializer for TimeDeltaSerializer {
exclude: Option<&Bound<'_, PyAny>>,
extra: &Extra,
) -> PyResult<Py<PyAny>> {
match extra.mode {
SerMode::Json => match EitherTimedelta::try_from(value) {
Ok(either_timedelta) => Ok(self.temporal_mode.timedelta_to_json(value.py(), either_timedelta)?),
Err(_) => {
extra.warnings.on_fallback_py(self.get_name(), value, extra)?;
infer_to_python(value, include, exclude, extra)
}
match EitherTimedelta::try_from(value) {
Ok(either_timedelta) => match extra.mode {
SerMode::Json => Ok(self.temporal_mode.timedelta_to_json(value.py(), either_timedelta)?),
_ => Ok(value.clone().unbind()),
},
_ => infer_to_python(value, include, exclude, extra),
_ => {
extra.warnings.on_fallback_py(self.get_name(), value, extra)?;
infer_to_python(value, include, exclude, extra)
}
}
}

Expand Down
72 changes: 69 additions & 3 deletions tests/serializers/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ def test_config_datetime(
assert s.to_python(dt, mode='json') == expected_to_python
assert s.to_json(dt) == expected_to_json

assert s.to_python({dt: 'foo'}) == {dt: 'foo'}
with pytest.warns(
UserWarning,
match=(
r'Expected `datetime` - serialized value may not be as expected '
r"\[input_value=\{datetime\.datetime\([^)]*\): 'foo'\}, input_type=dict\]"
),
):
assert s.to_python({dt: 'foo'}) == {dt: 'foo'}
with pytest.warns(
UserWarning,
match=(
Expand Down Expand Up @@ -224,7 +231,14 @@ def test_config_date(
assert s.to_python(dt, mode='json') == expected_to_python
assert s.to_json(dt) == expected_to_json

assert s.to_python({dt: 'foo'}) == {dt: 'foo'}
with pytest.warns(
UserWarning,
match=(
r'Expected `date` - serialized value may not be as expected '
r"\[input_value=\{datetime\.date\([^)]*\): 'foo'\}, input_type=dict\]"
),
):
assert s.to_python({dt: 'foo'}) == {dt: 'foo'}
with pytest.warns(
UserWarning,
match=(
Expand Down Expand Up @@ -280,7 +294,14 @@ def test_config_time(
assert s.to_python(t, mode='json') == expected_to_python
assert s.to_json(t) == expected_to_json

assert s.to_python({t: 'foo'}) == {t: 'foo'}
with pytest.warns(
UserWarning,
match=(
r'Expected `time` - serialized value may not be as expected '
r"\[input_value=\{datetime\.time\([^)]*\): 'foo'\}, input_type=dict\]"
),
):
assert s.to_python({t: 'foo'}) == {t: 'foo'}
with pytest.warns(
UserWarning,
match=(
Expand All @@ -297,3 +318,48 @@ def test_config_time(
),
):
assert s.to_json({t: 'foo'}) == expected_to_json_dict


def test_union_datetime_downcasts_correctly():
serialization_schema = core_schema.plain_serializer_function_ser_schema(lambda v: None)
json_validation_schema = core_schema.no_info_plain_validator_function(
function=lambda v: v, serialization=serialization_schema
)

test_custom_ser_schema = core_schema.json_schema(
schema=json_validation_schema,
serialization=serialization_schema,
)

s = SchemaSerializer(core_schema.union_schema(choices=[core_schema.datetime_schema(), test_custom_ser_schema]))
assert s.to_python('foo') is None


def test_union_date_respects_downcasts_correctly():
serialization_schema = core_schema.plain_serializer_function_ser_schema(lambda v: None)
json_validation_schema = core_schema.no_info_plain_validator_function(
function=lambda v: v, serialization=serialization_schema
)

test_custom_ser_schema = core_schema.json_schema(
schema=json_validation_schema,
serialization=serialization_schema,
)

s = SchemaSerializer(core_schema.union_schema(choices=[core_schema.date_schema(), test_custom_ser_schema]))
assert s.to_python('foo') is None


def test_union_time_respects_downcasts_correctly():
serialization_schema = core_schema.plain_serializer_function_ser_schema(lambda v: None)
json_validation_schema = core_schema.no_info_plain_validator_function(
function=lambda v: v, serialization=serialization_schema
)

test_custom_ser_schema = core_schema.json_schema(
schema=json_validation_schema,
serialization=serialization_schema,
)

s = SchemaSerializer(core_schema.union_schema(choices=[core_schema.time_schema(), test_custom_ser_schema]))
assert s.to_python('foo') is None
29 changes: 26 additions & 3 deletions tests/serializers/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ def test_config_timedelta(
assert s.to_python(td) == td
assert s.to_python(td, mode='json') == expected_to_python
assert s.to_json(td) == expected_to_json
assert s.to_python({td: 'foo'}) == {td: 'foo'}

with pytest.warns(UserWarning):
assert s.to_python({td: 'foo'}) == {td: 'foo'}
with pytest.warns(UserWarning):
assert s.to_python({td: 'foo'}, mode='json') == expected_to_python_dict
with pytest.warns(
Expand Down Expand Up @@ -330,8 +332,14 @@ def test_config_timedelta_timedelta_ser_flag_prioritised(
)
assert s.to_python(td) == td
assert s.to_python(td, mode='json') == expected_to_python
assert s.to_python({td: 'foo'}) == {td: 'foo'}

with pytest.warns(
UserWarning,
match=(
r'Expected `timedelta` - serialized value may not be as expected '
r"\[input_value=\{datetime\.timedelta\([^)]*\): 'foo'\}, input_type=dict\]"
),
):
assert s.to_python({td: 'foo'}) == {td: 'foo'}
with pytest.warns(
UserWarning,
match=(
Expand All @@ -348,3 +356,18 @@ def test_config_timedelta_timedelta_ser_flag_prioritised(
),
):
assert s.to_json({td: 'foo'}) == expected_to_json_dict


def test_union_timedelta_respects_instanceof_check():
serialization_schema = core_schema.plain_serializer_function_ser_schema(lambda v: None)
json_validation_schema = core_schema.no_info_plain_validator_function(
function=lambda v: v, serialization=serialization_schema
)

test_custom_ser_schema = core_schema.json_schema(
schema=json_validation_schema,
serialization=serialization_schema,
)

s = SchemaSerializer(core_schema.union_schema(choices=[core_schema.timedelta_schema(), test_custom_ser_schema]))
assert s.to_python('foo') is None
Loading