Skip to content

Commit 63f108d

Browse files
committed
add check in test, simplify error message
1 parent b279824 commit 63f108d

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/serializers/fields.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ impl GeneralFieldsSerializer {
212212
&& self.required_fields > used_req_fields
213213
{
214214
let required_fields = self.required_fields;
215-
let field_name = extra.field_name.unwrap_or("<unknown field>").to_string();
216215
let type_name = match extra.model {
217216
Some(model) => model
218217
.get_type()
@@ -222,17 +221,14 @@ impl GeneralFieldsSerializer {
222221
.to_string(),
223222
None => "<unknown python object>".to_string(),
224223
};
225-
226224
let field_value = match extra.model {
227225
Some(model) => truncate_safe_repr(model, Some(100)),
228226
None => "<unknown python object>".to_string(),
229227
};
230228

231-
Err(PydanticSerializationUnexpectedValue::new_err(
232-
Some(format!(
233-
"Expected {required_fields} fields but got {used_req_fields} for field {field_name} of type `{type_name}` with value `{field_value}` - serialized value may not be as expected."
234-
))
235-
))
229+
Err(PydanticSerializationUnexpectedValue::new_err(Some(format!(
230+
"Expected {required_fields} fields but got {used_req_fields} for type `{type_name}` with value `{field_value}` - serialized value may not be as expected."
231+
))))
236232
} else {
237233
Ok(output_dict)
238234
}

tests/serializers/test_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,8 @@ class BModel(BasicModel): ...
11501150
)
11511151
)
11521152

1153-
with pytest.raises(PydanticSerializationUnexpectedValue):
1153+
with pytest.raises(
1154+
PydanticSerializationUnexpectedValue, match='Expected 2 fields but got 1 for type `.*AModel` with value `.*`.+'
1155+
):
11541156
value = BasicModel(root=AModel(type='a'))
11551157
s.to_python(value)

0 commit comments

Comments
 (0)