diff --git a/tests/serializers/test_model.py b/tests/serializers/test_model.py index 9735cd0d3..9fa44032a 100644 --- a/tests/serializers/test_model.py +++ b/tests/serializers/test_model.py @@ -15,7 +15,6 @@ from pydantic_core import ( PydanticSerializationError, - PydanticSerializationUnexpectedValue, SchemaSerializer, SchemaValidator, core_schema, @@ -1150,8 +1149,6 @@ class BModel(BasicModel): ... ) ) - with pytest.raises( - PydanticSerializationUnexpectedValue, match='Expected 2 fields but got 1 for type `.*AModel` with value `.*`.+' - ): + with pytest.warns(UserWarning, match='Expected 2 fields but got 1 for type `.*AModel` with value `.*`.+'): value = BasicModel(root=AModel(type='a')) s.to_python(value) diff --git a/tests/serializers/test_union.py b/tests/serializers/test_union.py index 342f12cf4..8b6d6f128 100644 --- a/tests/serializers/test_union.py +++ b/tests/serializers/test_union.py @@ -1,7 +1,7 @@ import dataclasses import json -import re import uuid +import warnings from decimal import Decimal from typing import Any, ClassVar, Union @@ -32,9 +32,17 @@ def test_union_bool_int(input_value, expected_value, bool_case_label, int_case_l def test_union_error(): s = SchemaSerializer(core_schema.union_schema([core_schema.bool_schema(), core_schema.int_schema()])) - msg = "Expected `Union[bool, int]` but got `str` with value `'a string'` - serialized value may not be as expected" - with pytest.warns(UserWarning, match=re.escape(msg)): - assert s.to_python('a string') == 'a string' + + messages = [ + "Expected `bool` but got `str` with value `'a string'` - serialized value may not be as expected", + "Expected `int` but got `str` with value `'a string'` - serialized value may not be as expected", + ] + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + s.to_python('a string') + for m in messages: + assert m in str(w[0].message) class ModelA: