Skip to content

Commit 747a293

Browse files
committed
fix tests and message formatting
1 parent 4db4ec8 commit 747a293

20 files changed

+202
-113
lines changed

src/serializers/errors.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,33 @@ impl PydanticSerializationUnexpectedValue {
148148
}
149149

150150
pub(crate) fn __str__(&self, py: Python) -> String {
151-
let mut message = self.message.as_deref().unwrap_or("Unexpected Value").to_string();
151+
let mut message = self.message.as_deref().unwrap_or("").to_string();
152152

153153
if let Some(field_type) = &self.field_type {
154+
if !message.is_empty() {
155+
message.push_str(": ");
156+
}
154157
message.push_str(&format!("Expected `{field_type}`"));
158+
if self.input_value.is_some() {
159+
message.push_str(" - serialized value may not be as expected");
160+
}
155161
}
156162

157163
if let Some(input_value) = &self.input_value {
158164
let bound_input = input_value.bind(py);
159-
let type_name = bound_input
165+
let input_type = bound_input
160166
.get_type()
161167
.name()
162168
.unwrap_or_else(|_| PyString::new(py, "<unknown python object>"))
163169
.to_string();
164170

165171
let value_str = truncate_safe_repr(bound_input, None);
166172

167-
message.push_str(&format!(" but got `{type_name}` with value `{value_str}`"));
173+
message.push_str(&format!(" [input_value={value_str}, input_type={input_type}]"));
168174
}
169175

170-
if self.input_value.is_some() || self.field_type.is_some() {
171-
message.push_str(" - serialized value may not be as expected.");
176+
if message.is_empty() {
177+
message = "Unexpected Value".to_string();
172178
}
173179

174180
message

src/serializers/fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl GeneralFieldsSerializer {
219219
let required_fields = self.required_fields;
220220

221221
Err(PydanticSerializationUnexpectedValue::new(
222-
Some(format!("Expected {required_fields} fields but got {used_req_fields}. ").to_string()),
222+
Some(format!("Expected {required_fields} fields but got {used_req_fields}").to_string()),
223223
extra.model_type_name().map(|bound| bound.to_string()),
224224
extra.model.map(|bound| bound.clone().unbind()),
225225
)

tests/serializers/test_any.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_any_with_date_serializer():
164164
assert s.to_python(b'bang', mode='json') == 'bang'
165165

166166
assert (
167-
"Expected `date` but got `bytes` with value `b'bang'` - serialized value may not be as expected"
167+
"Expected `date` - serialized value may not be as expected [input_value=b'bang', input_type=bytes]"
168168
in warning_info.list[0].message.args[0]
169169
)
170170

@@ -179,7 +179,7 @@ def test_any_with_timedelta_serializer():
179179
assert s.to_python(b'bang', mode='json') == 'bang'
180180

181181
assert (
182-
"Expected `timedelta` but got `bytes` with value `b'bang'` - serialized value may not be as expected"
182+
"Expected `timedelta` - serialized value may not be as expected [input_value=b'bang', input_type=bytes]"
183183
in warning_info.list[0].message.args[0]
184184
)
185185

tests/serializers/test_bytes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,23 @@ def test_bytes_dict_key():
4747
def test_bytes_fallback():
4848
s = SchemaSerializer(core_schema.bytes_schema())
4949
with pytest.warns(
50-
UserWarning, match='Expected `bytes` but got `int` with value `123` - serialized value may not be as expected'
50+
UserWarning,
51+
match=r'Expected `bytes` - serialized value may not be as expected \[input_value=123, input_type=int\]',
5152
):
5253
assert s.to_python(123) == 123
5354
with pytest.warns(
54-
UserWarning, match='Expected `bytes` but got `int` with value `123` - serialized value may not be as expected'
55+
UserWarning,
56+
match=r'Expected `bytes` - serialized value may not be as expected \[input_value=123, input_type=int\]',
5557
):
5658
assert s.to_python(123, mode='json') == 123
5759
with pytest.warns(
58-
UserWarning, match='Expected `bytes` but got `int` with value `123` - serialized value may not be as expected'
60+
UserWarning,
61+
match=r'Expected `bytes` - serialized value may not be as expected \[input_value=123, input_type=int\]',
5962
):
6063
assert s.to_json(123) == b'123'
6164
with pytest.warns(
62-
UserWarning, match="Expected `bytes` but got `str` with value `'foo'` - serialized value may not be as expected"
65+
UserWarning,
66+
match=r"Expected `bytes` - serialized value may not be as expected \[input_value='foo', input_type=str\]",
6367
):
6468
assert s.to_json('foo') == b'"foo"'
6569

tests/serializers/test_datetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ def test_datetime():
1414

1515
with pytest.warns(
1616
UserWarning,
17-
match='Expected `datetime` but got `int` with value `123` - serialized value may not be as expected',
17+
match=r'Expected `datetime` - serialized value may not be as expected \[input_value=123, input_type=int\]',
1818
):
1919
assert v.to_python(123, mode='json') == 123
2020

2121
with pytest.warns(
2222
UserWarning,
23-
match='Expected `datetime` but got `int` with value `123` - serialized value may not be as expected',
23+
match=r'Expected `datetime` - serialized value may not be as expected \[input_value=123, input_type=int\]',
2424
):
2525
assert v.to_json(123) == b'123'
2626

tests/serializers/test_decimal.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def test_decimal():
2121
)
2222

2323
with pytest.warns(
24-
UserWarning, match='Expected `decimal` but got `int` with value `123` - serialized value may not be as expected'
24+
UserWarning,
25+
match=r'Expected `decimal` - serialized value may not be as expected \[input_value=123, input_type=int\]',
2526
):
2627
assert v.to_python(123, mode='json') == 123
2728

2829
with pytest.warns(
29-
UserWarning, match='Expected `decimal` but got `int` with value `123` - serialized value may not be as expected'
30+
UserWarning,
31+
match=r'Expected `decimal` - serialized value may not be as expected \[input_value=123, input_type=int\]',
3032
):
3133
assert v.to_json(123) == b'123'
3234

tests/serializers/test_enum.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class MyEnum(Enum):
1818
assert v.to_json(MyEnum.a) == b'1'
1919

2020
with pytest.warns(
21-
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
21+
UserWarning,
22+
match=r'Expected `enum` - serialized value may not be as expected \[input_value=1, input_type=int\]',
2223
):
2324
assert v.to_python(1) == 1
2425
with pytest.warns(
25-
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
26+
UserWarning,
27+
match=r'Expected `enum` - serialized value may not be as expected \[input_value=1, input_type=int\]',
2628
):
2729
assert v.to_json(1) == b'1'
2830

@@ -40,11 +42,13 @@ class MyEnum(int, Enum):
4042
assert v.to_json(MyEnum.a) == b'1'
4143

4244
with pytest.warns(
43-
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
45+
UserWarning,
46+
match=r'Expected `enum` - serialized value may not be as expected \[input_value=1, input_type=int\]',
4447
):
4548
assert v.to_python(1) == 1
4649
with pytest.warns(
47-
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
50+
UserWarning,
51+
match=r'Expected `enum` - serialized value may not be as expected \[input_value=1, input_type=int\]',
4852
):
4953
assert v.to_json(1) == b'1'
5054

@@ -62,11 +66,13 @@ class MyEnum(str, Enum):
6266
assert v.to_json(MyEnum.a) == b'"a"'
6367

6468
with pytest.warns(
65-
UserWarning, match="Expected `enum` but got `str` with value `'a'` - serialized value may not be as expected"
69+
UserWarning,
70+
match=r"Expected `enum` - serialized value may not be as expected \[input_value='a', input_type=str\]",
6671
):
6772
assert v.to_python('a') == 'a'
6873
with pytest.warns(
69-
UserWarning, match="Expected `enum` but got `str` with value `'a'` - serialized value may not be as expected"
74+
UserWarning,
75+
match=r"Expected `enum` - serialized value may not be as expected \[input_value='a', input_type=str\]",
7076
):
7177
assert v.to_json('a') == b'"a"'
7278

@@ -89,11 +95,13 @@ class MyEnum(Enum):
8995
assert v.to_json({MyEnum.a: 'x'}) == b'{"1":"x"}'
9096

9197
with pytest.warns(
92-
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
98+
UserWarning,
99+
match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
93100
):
94101
assert v.to_python({'x': 'x'}) == {'x': 'x'}
95102
with pytest.warns(
96-
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
103+
UserWarning,
104+
match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
97105
):
98106
assert v.to_json({'x': 'x'}) == b'{"x":"x"}'
99107

@@ -116,10 +124,12 @@ class MyEnum(int, Enum):
116124
assert v.to_json({MyEnum.a: 'x'}) == b'{"1":"x"}'
117125

118126
with pytest.warns(
119-
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
127+
UserWarning,
128+
match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
120129
):
121130
assert v.to_python({'x': 'x'}) == {'x': 'x'}
122131
with pytest.warns(
123-
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
132+
UserWarning,
133+
match=r"Expected `enum` - serialized value may not be as expected \[input_value='x', input_type=str\]",
124134
):
125135
assert v.to_json({'x': 'x'}) == b'{"x":"x"}'

tests/serializers/test_functions.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def append_42(value, _info):
207207
assert s.to_python([1, 2, 3], mode='json') == [1, 2, 3, 42]
208208
assert s.to_json([1, 2, 3]) == b'[1,2,3,42]'
209209

210-
msg = r"Expected `list\[int\]` but got `str` with value `'abc'` - serialized value may not be as expected"
210+
msg = r"Expected `list\[int\]` - serialized value may not be as expected \[input_value='abc', input_type=str\]"
211211
with pytest.warns(UserWarning, match=msg):
212212
assert s.to_python('abc') == 'abc'
213213

@@ -323,15 +323,18 @@ def test_wrong_return_type():
323323
)
324324
)
325325
with pytest.warns(
326-
UserWarning, match="Expected `int` but got `str` with value `'123'` - serialized value may not be as expected"
326+
UserWarning,
327+
match=r"Expected `int` - serialized value may not be as expected \[input_value='123', input_type=str\]",
327328
):
328329
assert s.to_python(123) == '123'
329330
with pytest.warns(
330-
UserWarning, match="Expected `int` but got `str` with value `'123'` - serialized value may not be as expected"
331+
UserWarning,
332+
match=r"Expected `int` - serialized value may not be as expected \[input_value='123', input_type=str\]",
331333
):
332334
assert s.to_python(123, mode='json') == '123'
333335
with pytest.warns(
334-
UserWarning, match="Expected `int` but got `str` with value `'123'` - serialized value may not be as expected"
336+
UserWarning,
337+
match=r"Expected `int` - serialized value may not be as expected \[input_value='123', input_type=str\]",
335338
):
336339
assert s.to_json(123) == b'"123"'
337340

@@ -363,15 +366,18 @@ def f(value, serializer):
363366
assert s.to_python(3, mode='json') == 'result=3'
364367
assert s.to_json(3) == b'"result=3"'
365368
with pytest.warns(
366-
UserWarning, match='Expected `str` but got `int` with value `42` - serialized value may not be as expected'
369+
UserWarning,
370+
match=r'Expected `str` - serialized value may not be as expected \[input_value=42, input_type=int\]',
367371
):
368372
assert s.to_python(42) == 42
369373
with pytest.warns(
370-
UserWarning, match='Expected `str` but got `int` with value `42` - serialized value may not be as expected'
374+
UserWarning,
375+
match=r'Expected `str` - serialized value may not be as expected \[input_value=42, input_type=int\]',
371376
):
372377
assert s.to_python(42, mode='json') == 42
373378
with pytest.warns(
374-
UserWarning, match='Expected `str` but got `int` with value `42` - serialized value may not be as expected'
379+
UserWarning,
380+
match=r'Expected `str` - serialized value may not be as expected \[input_value=42, input_type=int\]',
375381
):
376382
assert s.to_json(42) == b'42'
377383

@@ -624,7 +630,8 @@ def f(value, _info):
624630

625631
s = SchemaSerializer(core_schema.with_info_after_validator_function(f, core_schema.int_schema()))
626632
with pytest.warns(
627-
UserWarning, match="Expected `int` but got `str` with value `'abc'` - serialized value may not be as expected"
633+
UserWarning,
634+
match=r"Expected `int` - serialized value may not be as expected \[input_value='abc', input_type=str\]",
628635
):
629636
assert s.to_python('abc') == 'abc'
630637

@@ -635,7 +642,8 @@ def f(value, handler, _info):
635642

636643
s = SchemaSerializer(core_schema.with_info_wrap_validator_function(f, core_schema.int_schema()))
637644
with pytest.warns(
638-
UserWarning, match="Expected `int` but got `str` with value `'abc'` - serialized value may not be as expected"
645+
UserWarning,
646+
match=r"Expected `int` - serialized value may not be as expected \[input_value='abc', input_type=str\]",
639647
):
640648
assert s.to_python('abc') == 'abc'
641649

tests/serializers/test_generator.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,20 @@ def test_generator_any():
5454
assert s.to_json(iter(['a', b'b', 3])) == b'["a","b",3]'
5555
assert s.to_json(gen_ok('a', b'b', 3)) == b'["a","b",3]'
5656

57-
msg = 'Expected `generator` but got `int` with value `4` - serialized value may not be as expected'
58-
with pytest.warns(UserWarning, match=msg):
57+
with pytest.warns(
58+
UserWarning,
59+
match=r'Expected `generator` - serialized value may not be as expected \[input_value=4, input_type=int\]',
60+
):
5961
assert s.to_python(4) == 4
60-
with pytest.warns(UserWarning, match="Expected `generator` but got `tuple` with value `\\('a', b'b', 3\\)`"):
62+
with pytest.warns(
63+
UserWarning,
64+
match=r"Expected `generator` - serialized value may not be as expected \[input_value=\('a', b'b', 3\), input_type=tuple\]",
65+
):
6166
assert s.to_python(('a', b'b', 3)) == ('a', b'b', 3)
62-
with pytest.warns(UserWarning, match="Expected `generator` but got `str` with value `'abc'`"):
67+
with pytest.warns(
68+
UserWarning,
69+
match=r"Expected `generator` - serialized value may not be as expected \[input_value='abc', input_type=str\]",
70+
):
6371
assert s.to_python('abc') == 'abc'
6472

6573
with pytest.raises(ValueError, match='oops'):
@@ -89,19 +97,21 @@ def test_generator_int():
8997
s.to_json(gen_error(1, 2))
9098

9199
with pytest.warns(
92-
UserWarning, match="Expected `int` but got `str` with value `'a'` - serialized value may not be as expected"
100+
UserWarning,
101+
match=r"Expected `int` - serialized value may not be as expected \[input_value='a', input_type=str\]",
93102
):
94103
s.to_json(gen_ok(1, 'a'))
95104

96105
gen = s.to_python(gen_ok(1, 'a'))
97106
assert next(gen) == 1
98107
with pytest.warns(
99-
UserWarning, match="Expected `int` but got `str` with value `'a'` - serialized value may not be as expected"
108+
UserWarning,
109+
match=r"Expected `int` - serialized value may not be as expected \[input_value='a', input_type=str\]",
100110
):
101111
assert next(gen) == 'a'
102112
with pytest.warns(
103113
UserWarning,
104-
match='Expected `generator` but got `tuple` with value `\\(1, 2, 3\\)` - serialized value may not.+',
114+
match=r'Expected `generator` - serialized value may not be as expected \[input_value=\(1, 2, 3\), input_type=tuple\]',
105115
):
106116
s.to_python((1, 2, 3))
107117

0 commit comments

Comments
 (0)