Skip to content

Commit 6d18fab

Browse files
committed
Fixups
1 parent db9509c commit 6d18fab

22 files changed

+50
-285
lines changed

benches/main.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ use test::{black_box, Bencher};
88
use pyo3::prelude::*;
99
use pyo3::types::{PyDict, PyString};
1010

11-
use _pydantic_core::{validate_core_schema, SchemaValidator};
11+
use _pydantic_core::SchemaValidator;
1212

1313
fn build_schema_validator_with_globals(
1414
py: Python,
1515
code: &CStr,
1616
globals: Option<&Bound<'_, PyDict>>,
1717
) -> SchemaValidator {
1818
let mut schema = py.eval(code, globals, None).unwrap().extract().unwrap();
19-
schema = validate_core_schema(&schema, None).unwrap().extract().unwrap();
2019
SchemaValidator::py_new(py, &schema, None).unwrap()
2120
}
2221

@@ -511,7 +510,6 @@ fn complete_model(bench: &mut Bencher) {
511510

512511
let complete_schema = py.import("complete_schema").unwrap();
513512
let mut schema = complete_schema.call_method0("schema").unwrap();
514-
schema = validate_core_schema(&schema, None).unwrap().extract().unwrap();
515513
let validator = SchemaValidator::py_new(py, &schema, None).unwrap();
516514

517515
let input = complete_schema.call_method0("input_data_lax").unwrap();
@@ -535,7 +533,6 @@ fn nested_model_using_definitions(bench: &mut Bencher) {
535533

536534
let complete_schema = py.import("nested_schema").unwrap();
537535
let mut schema = complete_schema.call_method0("schema_using_defs").unwrap();
538-
schema = validate_core_schema(&schema, None).unwrap().extract().unwrap();
539536
let validator = SchemaValidator::py_new(py, &schema, None).unwrap();
540537

541538
let input = complete_schema.call_method0("input_data_valid").unwrap();
@@ -563,7 +560,6 @@ fn nested_model_inlined(bench: &mut Bencher) {
563560

564561
let complete_schema = py.import("nested_schema").unwrap();
565562
let mut schema = complete_schema.call_method0("inlined_schema").unwrap();
566-
schema = validate_core_schema(&schema, None).unwrap().extract().unwrap();
567563
let validator = SchemaValidator::py_new(py, &schema, None).unwrap();
568564

569565
let input = complete_schema.call_method0("input_data_valid").unwrap();

python/pydantic_core/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from_json,
2626
to_json,
2727
to_jsonable_python,
28-
validate_core_schema,
2928
)
3029
from .core_schema import CoreConfig, CoreSchema, CoreSchemaType, ErrorType
3130

@@ -66,7 +65,6 @@
6665
'to_json',
6766
'from_json',
6867
'to_jsonable_python',
69-
'validate_core_schema',
7068
]
7169

7270

python/pydantic_core/_pydantic_core.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ __all__ = [
3434
'to_jsonable_python',
3535
'list_all_errors',
3636
'TzInfo',
37-
'validate_core_schema',
3837
]
3938
__version__: str
4039
build_profile: str

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use serializers::{
3939
to_json, to_jsonable_python, PydanticSerializationError, PydanticSerializationUnexpectedValue, SchemaSerializer,
4040
WarningsArg,
4141
};
42-
pub use validators::{validate_core_schema, PySome, SchemaValidator};
42+
pub use validators::{PySome, SchemaValidator};
4343

4444
use crate::input::Input;
4545

@@ -113,8 +113,8 @@ mod _pydantic_core {
113113

114114
#[pymodule_export]
115115
use crate::{
116-
from_json, list_all_errors, to_json, to_jsonable_python, validate_core_schema, ArgsKwargs, PyMultiHostUrl,
117-
PySome, PyUrl, PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticSerializationError,
116+
from_json, list_all_errors, to_json, to_jsonable_python, ArgsKwargs, PyMultiHostUrl, PySome, PyUrl,
117+
PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticSerializationError,
118118
PydanticSerializationUnexpectedValue, PydanticUndefinedType, PydanticUseDefault, SchemaError, SchemaSerializer,
119119
SchemaValidator, TzInfo, ValidationError,
120120
};

tests/benchmarks/complete_schema.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from decimal import Decimal
22

3+
from pydantic_core import CoreSchema
34

4-
def schema(*, strict: bool = False) -> dict:
5+
6+
def schema(*, strict: bool = False) -> CoreSchema:
57
class MyModel:
68
# __slots__ is not required, but it avoids __pydantic_fields_set__ falling into __dict__
79
__slots__ = '__dict__', '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__'
@@ -12,7 +14,7 @@ def append_func(input_value, info):
1214
def wrap_function(input_value, validator, info):
1315
return f'Input {validator(input_value)} Changed'
1416

15-
return {
17+
return { # type: ignore
1618
'type': 'model',
1719
'cls': MyModel,
1820
'config': {'strict': strict},
@@ -342,7 +344,7 @@ def input_data_wrong():
342344
}
343345

344346

345-
def wrap_schema_in_root_model(schema: dict) -> dict:
347+
def wrap_schema_in_root_model(schema: CoreSchema) -> CoreSchema:
346348
class MyRootModel:
347349
# __slots__ is not required, but it avoids __pydantic_fields_set__ falling into __dict__
348350
__slots__ = '__dict__', '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__'

tests/benchmarks/test_complete_benchmark.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
import pytest
1111

12-
from pydantic_core import SchemaSerializer, SchemaValidator, ValidationError, validate_core_schema
12+
from pydantic_core import SchemaSerializer, SchemaValidator, ValidationError
1313

1414
from .complete_schema import input_data_lax, input_data_strict, input_data_wrong, schema, wrap_schema_in_root_model
1515

1616

1717
def test_complete_valid():
1818
lax_schema = schema()
1919
cls = lax_schema['cls']
20-
lax_validator = SchemaValidator(validate_core_schema(lax_schema))
20+
lax_validator = SchemaValidator(lax_schema)
2121
output = lax_validator.validate_python(input_data_lax())
2222
assert isinstance(output, cls)
2323
assert len(output.__pydantic_fields_set__) == 41
@@ -74,40 +74,40 @@ def test_complete_valid():
7474
},
7575
}
7676

77-
strict_validator = SchemaValidator(validate_core_schema(schema(strict=True)))
77+
strict_validator = SchemaValidator(schema(strict=True))
7878
output2 = strict_validator.validate_python(input_data_strict())
7979
assert output_dict == output2.__dict__
8080

8181

8282
def test_complete_invalid():
8383
lax_schema = schema()
84-
lax_validator = SchemaValidator(validate_core_schema(lax_schema))
84+
lax_validator = SchemaValidator(lax_schema)
8585
with pytest.raises(ValidationError) as exc_info:
8686
lax_validator.validate_python(input_data_wrong())
8787
assert len(exc_info.value.errors(include_url=False)) == 739
8888

8989

9090
@pytest.mark.benchmark(group='complete')
9191
def test_complete_core_lax(benchmark):
92-
v = SchemaValidator(validate_core_schema(schema()))
92+
v = SchemaValidator(schema())
9393
benchmark(v.validate_python, input_data_lax())
9494

9595

9696
@pytest.mark.benchmark(group='complete')
9797
def test_complete_core_strict(benchmark):
98-
v = SchemaValidator(validate_core_schema(schema(strict=True)))
98+
v = SchemaValidator(schema(strict=True))
9999
benchmark(v.validate_python, input_data_strict())
100100

101101

102102
@pytest.mark.benchmark(group='complete')
103103
def test_complete_core_root(benchmark):
104-
v = SchemaValidator(validate_core_schema(wrap_schema_in_root_model(schema())))
104+
v = SchemaValidator(wrap_schema_in_root_model(schema()))
105105
benchmark(v.validate_python, {'root': input_data_lax()})
106106

107107

108108
@pytest.mark.benchmark(group='complete-to-python')
109109
def test_complete_core_serializer_to_python(benchmark):
110-
core_schema = validate_core_schema(schema())
110+
core_schema = schema()
111111
v = SchemaValidator(core_schema)
112112
model = v.validate_python(input_data_lax())
113113
serializer = SchemaSerializer(core_schema)
@@ -117,7 +117,7 @@ def test_complete_core_serializer_to_python(benchmark):
117117

118118
@pytest.mark.benchmark(group='complete-to-json')
119119
def test_complete_core_serializer_to_json(benchmark):
120-
core_schema = validate_core_schema(schema())
120+
core_schema = schema()
121121
v = SchemaValidator(core_schema)
122122
model = v.validate_python(input_data_lax())
123123
serializer = SchemaSerializer(core_schema)
@@ -126,7 +126,7 @@ def test_complete_core_serializer_to_json(benchmark):
126126

127127
@pytest.mark.benchmark(group='complete-wrong')
128128
def test_complete_core_error(benchmark):
129-
v = SchemaValidator(validate_core_schema(schema()))
129+
v = SchemaValidator(schema())
130130
data = input_data_wrong()
131131

132132
@benchmark
@@ -141,7 +141,7 @@ def f():
141141

142142
@pytest.mark.benchmark(group='complete-wrong')
143143
def test_complete_core_isinstance(benchmark):
144-
v = SchemaValidator(validate_core_schema(schema()))
144+
v = SchemaValidator(schema())
145145
data = input_data_wrong()
146146
assert v.isinstance_python(data) is False
147147

@@ -161,19 +161,19 @@ def default_json_encoder(obj):
161161

162162
@pytest.mark.benchmark(group='complete-json')
163163
def test_complete_core_json(benchmark):
164-
v = SchemaValidator(validate_core_schema(schema()))
164+
v = SchemaValidator(schema())
165165
json_data = json.dumps(input_data_lax(), default=default_json_encoder)
166166
benchmark(v.validate_json, json_data)
167167

168168

169169
@pytest.mark.benchmark(group='complete-json')
170170
def test_complete_core_root_json(benchmark):
171-
v = SchemaValidator(validate_core_schema(wrap_schema_in_root_model(schema())))
171+
v = SchemaValidator(wrap_schema_in_root_model(schema()))
172172
json_data = json.dumps({'root': input_data_lax()}, default=default_json_encoder)
173173
benchmark(v.validate_json, json_data)
174174

175175

176176
@pytest.mark.benchmark(group='build')
177177
def test_build_schema(benchmark):
178178
lax_schema = schema()
179-
benchmark(lambda s: SchemaValidator(validate_core_schema(s)), lax_schema)
179+
benchmark(SchemaValidator, lax_schema)

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import hypothesis
1616
import pytest
1717

18-
from pydantic_core import ArgsKwargs, CoreSchema, SchemaValidator, ValidationError, validate_core_schema
18+
from pydantic_core import ArgsKwargs, CoreSchema, SchemaValidator, ValidationError
1919
from pydantic_core.core_schema import CoreConfig
2020

2121
__all__ = 'Err', 'PyAndJson', 'assert_gc', 'is_free_threaded', 'plain_repr', 'infinite_generator'
@@ -64,7 +64,7 @@ def __init__(
6464
*,
6565
validator_type: Literal['json', 'python'] | None = None,
6666
):
67-
self.validator = SchemaValidator(validate_core_schema(schema), config)
67+
self.validator = SchemaValidator(schema, config)
6868
self.validator_type = validator_type
6969

7070
def validate_python(self, py_input, strict: bool | None = None, context: Any = None):

tests/serializers/test_definitions.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from pydantic_core import SchemaError, SchemaSerializer, core_schema, validate_core_schema
3+
from pydantic_core import SchemaError, SchemaSerializer, core_schema
44

55

66
def test_custom_ser():
@@ -23,20 +23,6 @@ def test_ignored_def():
2323
assert s.to_python([1, 2, 3]) == [1, 2, 3]
2424

2525

26-
def test_def_error():
27-
with pytest.raises(SchemaError) as exc_info:
28-
validate_core_schema(
29-
core_schema.definitions_schema(
30-
core_schema.list_schema(core_schema.definition_reference_schema('foobar')),
31-
[core_schema.int_schema(ref='foobar'), {'type': 'wrong'}],
32-
)
33-
)
34-
35-
assert str(exc_info.value).startswith(
36-
"Invalid Schema:\ndefinitions.definitions.1\n Input tag 'wrong' found using 'type'"
37-
)
38-
39-
4026
def test_repeated_ref():
4127
with pytest.raises(SchemaError, match='SchemaError: Duplicate ref: `foobar`'):
4228
SchemaSerializer(

tests/serializers/test_dict.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from dirty_equals import IsStrictDict
55

6-
from pydantic_core import SchemaError, SchemaSerializer, core_schema, validate_core_schema
6+
from pydantic_core import SchemaSerializer, core_schema
77

88

99
def test_dict_str_int():
@@ -143,18 +143,3 @@ def test_filter_runtime_int():
143143
core_schema.dict_schema(core_schema.any_schema(), serialization=core_schema.filter_dict_schema(exclude={0, 1}))
144144
)
145145
assert s.to_python({0: 0, 1: 1, 2: 2, 3: 3}, include={1, 2}) == {1: 1, 2: 2}
146-
147-
148-
@pytest.mark.parametrize(
149-
'include_value,error_msg',
150-
[
151-
('foobar', 'Input should be a valid set'),
152-
({'a': 'dict'}, 'Input should be a valid set'),
153-
({4.2}, 'Input should be a valid integer, got a number with a fractional part'),
154-
],
155-
)
156-
def test_include_error(include_value, error_msg):
157-
with pytest.raises(SchemaError, match=error_msg):
158-
validate_core_schema(
159-
core_schema.dict_schema(serialization=core_schema.filter_dict_schema(include=include_value))
160-
)

tests/serializers/test_list_tuple.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
from pydantic_core import (
77
PydanticSerializationError,
8-
SchemaError,
98
SchemaSerializer,
109
core_schema,
11-
validate_core_schema,
1210
)
1311

1412

@@ -162,13 +160,10 @@ def test_exclude(schema_func, seq_f):
162160
assert v.to_json(seq_f('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), exclude={-1, -2}) == b'["a","c","e"]'
163161

164162

165-
@pytest.mark.parametrize('include,exclude', [({1, 3, 5}, {5, 6}), ([1, 3, 5], [5, 6])])
166-
def test_filter(include, exclude):
163+
def test_filter():
167164
v = SchemaSerializer(
168-
validate_core_schema(
169-
core_schema.list_schema(
170-
core_schema.any_schema(), serialization=core_schema.filter_seq_schema(include=include, exclude=exclude)
171-
)
165+
core_schema.list_schema(
166+
core_schema.any_schema(), serialization=core_schema.filter_seq_schema(include={1, 3, 5}, exclude={5, 6})
172167
)
173168
)
174169
assert v.to_python([0, 1, 2, 3, 4, 5, 6, 7]) == [1, 3]
@@ -197,23 +192,6 @@ class RemovedContains(ImplicitContains):
197192
__contains__ = None # This might be done to explicitly force the `x in RemovedContains()` check to not be allowed
198193

199194

200-
@pytest.mark.parametrize(
201-
'include_value,error_msg',
202-
[
203-
('foobar', 'Input should be a valid set'),
204-
({'a': 'dict'}, 'Input should be a valid set'),
205-
({4.2}, 'Input should be a valid integer, got a number with a fractional part'),
206-
({'a'}, 'Input should be a valid integer, unable to parse string as an integer'),
207-
],
208-
)
209-
@pytest.mark.parametrize('schema_func', [core_schema.list_schema, core_schema.tuple_variable_schema])
210-
def test_include_error(schema_func, include_value, error_msg):
211-
with pytest.raises(SchemaError, match=error_msg):
212-
validate_core_schema(
213-
schema_func(core_schema.any_schema(), serialization=core_schema.filter_seq_schema(include=include_value))
214-
)
215-
216-
217195
@pytest.mark.parametrize(
218196
'include,exclude,expected',
219197
[

tests/serializers/test_misc.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/validators/test_custom_error.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from pydantic_core import SchemaError, SchemaValidator, ValidationError, core_schema, validate_core_schema
3+
from pydantic_core import SchemaError, SchemaValidator, ValidationError, core_schema
44

55
from ..conftest import PyAndJson
66

@@ -31,11 +31,6 @@ def test_custom_error_type(py_and_json: PyAndJson):
3131
]
3232

3333

34-
def test_custom_error_error():
35-
with pytest.raises(SchemaError, match=r'custom_error_type\s+Field required \[type=missing'):
36-
validate_core_schema({'type': 'custom-error', 'schema': {'type': 'int'}})
37-
38-
3934
def test_custom_error_invalid():
4035
msg = "custom_error_message should not be provided if 'custom_error_type' matches a known error"
4136
with pytest.raises(SchemaError, match=msg):

0 commit comments

Comments
 (0)