Skip to content

add default comparison #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,7 @@ def with_default_schema(
*,
default: Any = PydanticUndefined,
default_factory: Callable[[], Any] | None = None,
default_comparison: Callable[[Any, Any], bool] | None = None,
on_error: Literal['raise', 'omit', 'default'] | None = None,
validate_default: bool | None = None,
strict: bool | None = None,
Expand All @@ -2211,6 +2212,7 @@ def with_default_schema(
schema: The schema to add a default value to
default: The default value to use
default_factory: A function that returns the default value to use
default_comparison: A function to compare the default value with any other given
on_error: What to do if the schema validation fails. One of 'raise', 'omit', 'default'
validate_default: Whether the default value should be validated
strict: Whether the underlying schema should be validated with strict mode
Expand All @@ -2222,6 +2224,7 @@ def with_default_schema(
type='default',
schema=schema,
default_factory=default_factory,
default_comparison=default_comparison,
on_error=on_error,
validate_default=validate_default,
strict=strict,
Expand Down
7 changes: 2 additions & 5 deletions src/serializers/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ impl SerField {
}

fn exclude_default(value: &PyAny, extra: &Extra, serializer: &CombinedSerializer) -> PyResult<bool> {
let py = value.py();
if extra.exclude_defaults {
if let Some(default) = serializer.get_default(value.py())? {
if value.eq(default)? {
return Ok(true);
}
}
return serializer.compare_with_default(py, value);
}
Ok(false)
}
Expand Down
4 changes: 4 additions & 0 deletions src/serializers/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ pub(crate) trait TypeSerializer: Send + Sync + Clone + Debug {
fn get_default(&self, _py: Python) -> PyResult<Option<PyObject>> {
Ok(None)
}

fn compare_with_default(&self, _py: Python, _value: &PyAny) -> PyResult<bool> {
Ok(false)
}
}

pub(crate) struct PydanticSerializer<'py> {
Expand Down
22 changes: 20 additions & 2 deletions src/serializers/type_serializers/with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{BuildSerializer, CombinedSerializer, Extra, TypeSerializer};
#[derive(Debug, Clone)]
pub struct WithDefaultSerializer {
default: DefaultType,
default_comparison: Option<PyObject>,
serializer: Box<CombinedSerializer>,
}

Expand All @@ -26,11 +27,16 @@ impl BuildSerializer for WithDefaultSerializer {
) -> PyResult<CombinedSerializer> {
let py = schema.py();
let default = DefaultType::new(schema)?;

let default_comparison = schema.get_as(intern!(py, "default_comparison"))?;
let sub_schema: &PyDict = schema.get_as_req(intern!(py, "schema"))?;
let serializer = Box::new(CombinedSerializer::build(sub_schema, config, definitions)?);

Ok(Self { default, serializer }.into())
Ok(Self {
default,
default_comparison,
serializer,
}
.into())
}
}

Expand Down Expand Up @@ -74,4 +80,16 @@ impl TypeSerializer for WithDefaultSerializer {
fn get_default(&self, py: Python) -> PyResult<Option<PyObject>> {
self.default.default_value(py)
}

fn compare_with_default(&self, py: Python, value: &PyAny) -> PyResult<bool> {
if let Some(default) = self.get_default(py)? {
if let Some(default_comparison) = &self.default_comparison {
return default_comparison.call(py, (value, default), None)?.extract::<bool>(py);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm passing the values as positional here, not sure if is the best way to call the comparison operator

} else if value.eq(default)? {
return Ok(true);
}
}

Ok(false)
}
}
61 changes: 50 additions & 11 deletions tests/serializers/test_typed_dict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any, Dict
from typing import Any, Dict, List, Union

import pytest
from dirty_equals import IsStrictDict
Expand Down Expand Up @@ -150,24 +150,63 @@ def test_exclude_none():


def test_exclude_default():
v = SchemaSerializer(
core_schema.typed_dict_schema(
{
'foo': core_schema.typed_dict_field(core_schema.nullable_schema(core_schema.int_schema())),
'bar': core_schema.typed_dict_field(
core_schema.with_default_schema(core_schema.bytes_schema(), default=b'[default]')
),
}
)
class TestComparison:
def __init__(self, array: Union[List[int], List[float]]):
self.array = array

def __eq__(self, other):
"""Simple comparison arrays have to match also in the dtype"""
# Test case we just look at the first element to get the dtype of the array
self_is_integer = isinstance(self.array[0], int)
other_is_integer = isinstance(other.array[0], int)
return self_is_integer == other_is_integer and self.array == other.array

def custom_comparison_operator(value: TestComparison, default: TestComparison):
"""Will replace __eq__ in TestComparison omiting the dtype check"""
return value.array == default.array

dict_schema = core_schema.typed_dict_schema(
{
'foo': core_schema.typed_dict_field(core_schema.nullable_schema(core_schema.int_schema())),
'bar': core_schema.typed_dict_field(
core_schema.with_default_schema(core_schema.bytes_schema(), default=b'[default]')
),
'foobar': core_schema.typed_dict_field(
core_schema.with_default_schema(
core_schema.any_schema(),
default=TestComparison(array=[1, 2, 3]),
default_comparison=custom_comparison_operator,
)
),
}
)

v = SchemaSerializer(dict_schema)
assert v.to_python({'foo': 1, 'bar': b'x'}) == {'foo': 1, 'bar': b'x'}
assert v.to_python({'foo': 1, 'bar': b'[default]'}) == {'foo': 1, 'bar': b'[default]'}
assert v.to_python({'foo': 1, 'bar': b'[default]'}, exclude_defaults=True) == {'foo': 1}
assert v.to_python({'foo': 1, 'bar': b'[default]'}, mode='json') == {'foo': 1, 'bar': '[default]'}
assert v.to_python({'foo': 1, 'bar': b'[default]'}, exclude_defaults=True, mode='json') == {'foo': 1}

assert v.to_json({'foo': 1, 'bar': b'[default]'}) == b'{"foo":1,"bar":"[default]"}'
assert v.to_json({'foo': 1, 'bar': b'[default]'}, exclude_defaults=True) == b'{"foo":1}'
# Note that due to the custom comparison operator foobar must be excluded because this operator doesn't pay attention on the dtype
assert v.to_python(
{'foo': 1, 'bar': b'x', 'foobar': TestComparison(array=[1.0, 2.0, 3.0])}, exclude_defaults=True
) == {
'foo': 1,
'bar': b'x',
}
# Now removing custom comparison operator foobar must be included due that TestComparison.__eq__ checks the array dtype
# So TestComparison(array=[1.0, 2.0, 3.0]) is not equal to the default TestComparison(array=[1, 2, 3])
del dict_schema['fields']['foobar']['schema']['default_comparison']
v = SchemaSerializer(dict_schema)
assert v.to_python(
{'foo': 1, 'bar': b'x', 'foobar': TestComparison(array=[1.0, 2.0, 3.0])}, exclude_defaults=True
) == {
'foo': 1,
'bar': b'x',
'foobar': TestComparison(array=[1.0, 2.0, 3.0]),
}


def test_function_plain_field_serializer_to_python():
Expand Down