Skip to content

Commit 7af4f9c

Browse files
committed
Fix tests
1 parent f4ccac2 commit 7af4f9c

File tree

5 files changed

+31
-59
lines changed

5 files changed

+31
-59
lines changed

python/pydantic_core/core_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4251,6 +4251,7 @@ def definition_reference_schema(
42514251
'value_error',
42524252
'assertion_error',
42534253
'literal_error',
4254+
'unset_sentinel_error',
42544255
'date_type',
42554256
'date_parsing',
42564257
'date_from_datetime_parsing',

tests/serializers/test_model.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,34 @@ def volume(self) -> int:
744744
assert s.to_json(Model(3, 4), by_alias=True) == b'{"width":3,"height":4,"Area":12,"volume":48}'
745745

746746

747+
def test_computed_field_without_fields() -> None:
748+
"""https://github.com/pydantic/pydantic/issues/5551"""
749+
750+
# Original test introduced in https://github.com/pydantic/pydantic-core/pull/550
751+
752+
class A:
753+
@property
754+
def b(self) -> str:
755+
return 'b'
756+
757+
schema = core_schema.model_schema(
758+
cls=A,
759+
config={},
760+
schema=core_schema.model_fields_schema(
761+
fields={},
762+
computed_fields=[
763+
core_schema.computed_field('b', return_schema=core_schema.any_schema()),
764+
],
765+
),
766+
)
767+
768+
a = A()
769+
770+
serializer = SchemaSerializer(schema)
771+
772+
assert serializer.to_json(a) == b'{"b":"b"}'
773+
774+
747775
def test_computed_field_exclude_none():
748776
@dataclasses.dataclass
749777
class Model:

tests/test.rs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -53,65 +53,6 @@ mod tests {
5353
});
5454
}
5555

56-
#[test]
57-
fn test_serialize_computed_fields() {
58-
Python::with_gil(|py| {
59-
let code = c_str!(
60-
r#"
61-
class A:
62-
@property
63-
def b(self) -> str:
64-
return "b"
65-
66-
schema = {
67-
"cls": A,
68-
"config": {},
69-
"schema": {
70-
"computed_fields": [
71-
{"property_name": "b", "return_schema": {"type": "any"}, "type": "computed-field"}
72-
],
73-
"fields": {},
74-
"type": "model-fields",
75-
},
76-
"type": "model",
77-
}
78-
a = A()
79-
"#
80-
);
81-
let locals = PyDict::new(py);
82-
py.run(code, None, Some(&locals)).unwrap();
83-
let a = locals.get_item("a").unwrap().unwrap();
84-
let schema = locals
85-
.get_item("schema")
86-
.unwrap()
87-
.unwrap()
88-
.downcast_into::<PyDict>()
89-
.unwrap();
90-
let serialized = SchemaSerializer::py_new(schema, None)
91-
.unwrap()
92-
.to_json(
93-
py,
94-
&a,
95-
None,
96-
Some(false),
97-
None,
98-
None,
99-
Some(true),
100-
false,
101-
false,
102-
false,
103-
false,
104-
WarningsArg::Bool(true),
105-
None,
106-
false,
107-
None,
108-
)
109-
.unwrap();
110-
let serialized: &[u8] = serialized.extract(py).unwrap();
111-
assert_eq!(serialized, b"{\"b\":\"b\"}");
112-
});
113-
}
114-
11556
#[test]
11657
fn test_literal_schema() {
11758
Python::with_gil(|py| {

tests/test_errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def f(input_value, info):
340340
('assertion_error', 'Assertion failed, foobar', {'error': AssertionError('foobar')}),
341341
('literal_error', 'Input should be foo', {'expected': 'foo'}),
342342
('literal_error', 'Input should be foo or bar', {'expected': 'foo or bar'}),
343+
('unset_sentinel_error', "Input should be the 'UNSET' sentinel", None),
343344
('date_type', 'Input should be a valid date', None),
344345
('date_parsing', 'Input should be a valid date in the format YYYY-MM-DD, foobar', {'error': 'foobar'}),
345346
('date_from_datetime_parsing', 'Input should be a valid date or datetime, foobar', {'error': 'foobar'}),

tests/test_schema_functions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def args(*args, **kwargs):
8282
{'type': 'timedelta', 'microseconds_precision': 'error'},
8383
),
8484
(core_schema.literal_schema, args(['a', 'b']), {'type': 'literal', 'expected': ['a', 'b']}),
85+
(core_schema.unset_sentinel_schema, args(), {'type': 'unset-sentinel'}),
8586
(
8687
core_schema.enum_schema,
8788
args(MyEnum, list(MyEnum.__members__.values())),

0 commit comments

Comments
 (0)