@@ -168,7 +168,14 @@ def test_config_datetime(
168168 assert s .to_python (dt , mode = 'json' ) == expected_to_python
169169 assert s .to_json (dt ) == expected_to_json
170170
171- assert s .to_python ({dt : 'foo' }) == {dt : 'foo' }
171+ with pytest .warns (
172+ UserWarning ,
173+ match = (
174+ r'Expected `datetime` - serialized value may not be as expected '
175+ r"\[input_value=\{datetime\.datetime\([^)]*\): 'foo'\}, input_type=dict\]"
176+ ),
177+ ):
178+ assert s .to_python ({dt : 'foo' }) == {dt : 'foo' }
172179 with pytest .warns (
173180 UserWarning ,
174181 match = (
@@ -224,7 +231,14 @@ def test_config_date(
224231 assert s .to_python (dt , mode = 'json' ) == expected_to_python
225232 assert s .to_json (dt ) == expected_to_json
226233
227- assert s .to_python ({dt : 'foo' }) == {dt : 'foo' }
234+ with pytest .warns (
235+ UserWarning ,
236+ match = (
237+ r'Expected `date` - serialized value may not be as expected '
238+ r"\[input_value=\{datetime\.date\([^)]*\): 'foo'\}, input_type=dict\]"
239+ ),
240+ ):
241+ assert s .to_python ({dt : 'foo' }) == {dt : 'foo' }
228242 with pytest .warns (
229243 UserWarning ,
230244 match = (
@@ -280,7 +294,14 @@ def test_config_time(
280294 assert s .to_python (t , mode = 'json' ) == expected_to_python
281295 assert s .to_json (t ) == expected_to_json
282296
283- assert s .to_python ({t : 'foo' }) == {t : 'foo' }
297+ with pytest .warns (
298+ UserWarning ,
299+ match = (
300+ r'Expected `time` - serialized value may not be as expected '
301+ r"\[input_value=\{datetime\.time\([^)]*\): 'foo'\}, input_type=dict\]"
302+ ),
303+ ):
304+ assert s .to_python ({t : 'foo' }) == {t : 'foo' }
284305 with pytest .warns (
285306 UserWarning ,
286307 match = (
@@ -297,3 +318,48 @@ def test_config_time(
297318 ),
298319 ):
299320 assert s .to_json ({t : 'foo' }) == expected_to_json_dict
321+
322+
323+ def test_union_datetime_downcasts_correctly ():
324+ serialization_schema = core_schema .plain_serializer_function_ser_schema (lambda v : None )
325+ json_validation_schema = core_schema .no_info_plain_validator_function (
326+ function = lambda v : v , serialization = serialization_schema
327+ )
328+
329+ test_custom_ser_schema = core_schema .json_schema (
330+ schema = json_validation_schema ,
331+ serialization = serialization_schema ,
332+ )
333+
334+ s = SchemaSerializer (core_schema .union_schema (choices = [core_schema .datetime_schema (), test_custom_ser_schema ]))
335+ assert s .to_python ('foo' ) is None
336+
337+
338+ def test_union_date_respects_downcasts_correctly ():
339+ serialization_schema = core_schema .plain_serializer_function_ser_schema (lambda v : None )
340+ json_validation_schema = core_schema .no_info_plain_validator_function (
341+ function = lambda v : v , serialization = serialization_schema
342+ )
343+
344+ test_custom_ser_schema = core_schema .json_schema (
345+ schema = json_validation_schema ,
346+ serialization = serialization_schema ,
347+ )
348+
349+ s = SchemaSerializer (core_schema .union_schema (choices = [core_schema .date_schema (), test_custom_ser_schema ]))
350+ assert s .to_python ('foo' ) is None
351+
352+
353+ def test_union_time_respects_downcasts_correctly ():
354+ serialization_schema = core_schema .plain_serializer_function_ser_schema (lambda v : None )
355+ json_validation_schema = core_schema .no_info_plain_validator_function (
356+ function = lambda v : v , serialization = serialization_schema
357+ )
358+
359+ test_custom_ser_schema = core_schema .json_schema (
360+ schema = json_validation_schema ,
361+ serialization = serialization_schema ,
362+ )
363+
364+ s = SchemaSerializer (core_schema .union_schema (choices = [core_schema .time_schema (), test_custom_ser_schema ]))
365+ assert s .to_python ('foo' ) is None
0 commit comments