@@ -150,13 +150,27 @@ def test_exclude_none():
150150
151151
152152def test_exclude_default ():
153+ class TestComparison :
154+ def __init__ (self , val : Any ):
155+ self .val = val
156+
157+ def __eq__ (self , other ):
158+ return self .val == other .val
159+
153160 v = SchemaSerializer (
154161 core_schema .typed_dict_schema (
155162 {
156163 'foo' : core_schema .typed_dict_field (core_schema .nullable_schema (core_schema .int_schema ())),
157164 'bar' : core_schema .typed_dict_field (
158165 core_schema .with_default_schema (core_schema .bytes_schema (), default = b'[default]' )
159166 ),
167+ 'foobar' : core_schema .typed_dict_field (
168+ core_schema .with_default_schema (
169+ core_schema .any_schema (),
170+ default = TestComparison (val = 1 ),
171+ default_comparison = lambda value , default : value .val == - 1 * default .val ,
172+ )
173+ ),
160174 }
161175 )
162176 )
@@ -165,9 +179,19 @@ def test_exclude_default():
165179 assert v .to_python ({'foo' : 1 , 'bar' : b'[default]' }, exclude_defaults = True ) == {'foo' : 1 }
166180 assert v .to_python ({'foo' : 1 , 'bar' : b'[default]' }, mode = 'json' ) == {'foo' : 1 , 'bar' : '[default]' }
167181 assert v .to_python ({'foo' : 1 , 'bar' : b'[default]' }, exclude_defaults = True , mode = 'json' ) == {'foo' : 1 }
168-
169182 assert v .to_json ({'foo' : 1 , 'bar' : b'[default]' }) == b'{"foo":1,"bar":"[default]"}'
170183 assert v .to_json ({'foo' : 1 , 'bar' : b'[default]' }, exclude_defaults = True ) == b'{"foo":1}'
184+ # Note that due to the custom comparison operator foobar must be excluded
185+ assert v .to_python ({'foo' : 1 , 'bar' : b'x' , 'foobar' : TestComparison (val = - 1 )}, exclude_defaults = True ) == {
186+ 'foo' : 1 ,
187+ 'bar' : b'x' ,
188+ }
189+ # foobar here must be included
190+ assert v .to_python ({'foo' : 1 , 'bar' : b'x' , 'foobar' : TestComparison (val = 1 )}, exclude_defaults = True ) == {
191+ 'foo' : 1 ,
192+ 'bar' : b'x' ,
193+ 'foobar' : TestComparison (val = 1 ),
194+ }
171195
172196
173197def test_function_plain_field_serializer_to_python ():
0 commit comments