Skip to content

Commit e1c4af5

Browse files
committed
Rename validation and serialization schema types to be unique
`validator-` or `serializer-` is prepended, and the existing ones are deprecated.
1 parent db63fec commit e1c4af5

19 files changed

+183
-78
lines changed

python/pydantic_core/core_schema.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def simple_ser_schema(type: ExpectedSerializationTypes) -> SimpleSerSchema:
261261

262262

263263
class PlainSerializerFunctionSerSchema(TypedDict, total=False):
264-
type: Required[Literal['function-plain']]
264+
type: Required[Literal['serializer-function-plain']]
265265
function: Required[SerializerFunction]
266266
is_field_serializer: bool # default False
267267
info_arg: bool # default False
@@ -292,7 +292,7 @@ def plain_serializer_function_ser_schema(
292292
# just to avoid extra elements in schema, and to use the actual default defined in rust
293293
when_used = None # type: ignore
294294
return _dict_not_none(
295-
type='function-plain',
295+
type='serializer-function-plain',
296296
function=function,
297297
is_field_serializer=is_field_serializer,
298298
info_arg=info_arg,
@@ -322,7 +322,7 @@ def __call__(self, input_value: Any, index_key: int | str | None = None, /) -> A
322322

323323

324324
class WrapSerializerFunctionSerSchema(TypedDict, total=False):
325-
type: Required[Literal['function-wrap']]
325+
type: Required[Literal['serializer-function-wrap']]
326326
function: Required[WrapSerializerFunction]
327327
is_field_serializer: bool # default False
328328
info_arg: bool # default False
@@ -356,7 +356,7 @@ def wrap_serializer_function_ser_schema(
356356
# just to avoid extra elements in schema, and to use the actual default defined in rust
357357
when_used = None # type: ignore
358358
return _dict_not_none(
359-
type='function-wrap',
359+
type='serializer-function-wrap',
360360
function=function,
361361
is_field_serializer=is_field_serializer,
362362
info_arg=info_arg,
@@ -1966,7 +1966,7 @@ class _ValidatorFunctionSchema(TypedDict, total=False):
19661966

19671967

19681968
class BeforeValidatorFunctionSchema(_ValidatorFunctionSchema, total=False):
1969-
type: Required[Literal['function-before']]
1969+
type: Required[Literal['validator-function-before']]
19701970
json_schema_input_schema: CoreSchema
19711971

19721972

@@ -2006,7 +2006,7 @@ def fn(v: bytes) -> str:
20062006
serialization: Custom serialization schema
20072007
"""
20082008
return _dict_not_none(
2009-
type='function-before',
2009+
type='validator-function-before',
20102010
function={'type': 'no-info', 'function': function},
20112011
schema=schema,
20122012
ref=ref,
@@ -2057,7 +2057,7 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
20572057
serialization: Custom serialization schema
20582058
"""
20592059
return _dict_not_none(
2060-
type='function-before',
2060+
type='validator-function-before',
20612061
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
20622062
schema=schema,
20632063
ref=ref,
@@ -2068,7 +2068,7 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
20682068

20692069

20702070
class AfterValidatorFunctionSchema(_ValidatorFunctionSchema, total=False):
2071-
type: Required[Literal['function-after']]
2071+
type: Required[Literal['validator-function-after']]
20722072

20732073

20742074
def no_info_after_validator_function(
@@ -2105,7 +2105,7 @@ def fn(v: str) -> str:
21052105
serialization: Custom serialization schema
21062106
"""
21072107
return _dict_not_none(
2108-
type='function-after',
2108+
type='validator-function-after',
21092109
function={'type': 'no-info', 'function': function},
21102110
schema=schema,
21112111
ref=ref,
@@ -2154,7 +2154,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
21542154
serialization: Custom serialization schema
21552155
"""
21562156
return _dict_not_none(
2157-
type='function-after',
2157+
type='validator-function-after',
21582158
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
21592159
schema=schema,
21602160
ref=ref,
@@ -2191,7 +2191,7 @@ class WithInfoWrapValidatorFunctionSchema(TypedDict, total=False):
21912191

21922192

21932193
class WrapValidatorFunctionSchema(TypedDict, total=False):
2194-
type: Required[Literal['function-wrap']]
2194+
type: Required[Literal['validator-function-wrap']]
21952195
function: Required[WrapValidatorFunction]
21962196
schema: Required[CoreSchema]
21972197
ref: str
@@ -2239,7 +2239,7 @@ def fn(
22392239
serialization: Custom serialization schema
22402240
"""
22412241
return _dict_not_none(
2242-
type='function-wrap',
2242+
type='validator-function-wrap',
22432243
function={'type': 'no-info', 'function': function},
22442244
schema=schema,
22452245
json_schema_input_schema=json_schema_input_schema,
@@ -2291,7 +2291,7 @@ def fn(
22912291
serialization: Custom serialization schema
22922292
"""
22932293
return _dict_not_none(
2294-
type='function-wrap',
2294+
type='validator-function-wrap',
22952295
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
22962296
schema=schema,
22972297
json_schema_input_schema=json_schema_input_schema,
@@ -2302,7 +2302,7 @@ def fn(
23022302

23032303

23042304
class PlainValidatorFunctionSchema(TypedDict, total=False):
2305-
type: Required[Literal['function-plain']]
2305+
type: Required[Literal['validator-function-plain']]
23062306
function: Required[ValidationFunction]
23072307
ref: str
23082308
json_schema_input_schema: CoreSchema
@@ -2341,7 +2341,7 @@ def fn(v: str) -> str:
23412341
serialization: Custom serialization schema
23422342
"""
23432343
return _dict_not_none(
2344-
type='function-plain',
2344+
type='validator-function-plain',
23452345
function={'type': 'no-info', 'function': function},
23462346
ref=ref,
23472347
json_schema_input_schema=json_schema_input_schema,
@@ -2383,7 +2383,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
23832383
serialization: Custom serialization schema
23842384
"""
23852385
return _dict_not_none(
2386-
type='function-plain',
2386+
type='validator-function-plain',
23872387
function=_dict_not_none(type='with-info', function=function, field_name=field_name),
23882388
ref=ref,
23892389
json_schema_input_schema=json_schema_input_schema,
@@ -3965,10 +3965,10 @@ def definition_reference_schema(
39653965
'frozenset',
39663966
'generator',
39673967
'dict',
3968-
'function-after',
3969-
'function-before',
3970-
'function-wrap',
3971-
'function-plain',
3968+
'validator-function-after',
3969+
'validator-function-before',
3970+
'validator-function-wrap',
3971+
'validator-function-plain',
39723972
'default',
39733973
'nullable',
39743974
'union',

src/serializers/shared.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::ffi::CString;
23
use std::fmt::Debug;
34

45
use pyo3::exceptions::PyTypeError;
@@ -148,6 +149,29 @@ combined_serializer! {
148149
}
149150
}
150151

152+
fn get_new_type(py: Python, type_: &str) -> PyResult<String> {
153+
let maybe_new_type = match type_ {
154+
"function-plain" => Some("serializer-function-plain"),
155+
"function-wrap" => Some("serializer-function-wrap"),
156+
_ => None,
157+
};
158+
159+
match maybe_new_type {
160+
Some(new_type) => {
161+
let deprecation_message =
162+
format!("Serialization core schema type '{type_}' is deprecated, use '{new_type}'");
163+
let _ = PyErr::warn(
164+
py,
165+
&py.get_type::<pyo3::exceptions::PyDeprecationWarning>(),
166+
&CString::new(deprecation_message)?,
167+
1,
168+
);
169+
Ok(new_type.to_owned())
170+
}
171+
None => Ok(type_.to_owned()),
172+
}
173+
}
174+
151175
impl CombinedSerializer {
152176
fn _build(
153177
schema: &Bound<'_, PyDict>,
@@ -159,28 +183,35 @@ impl CombinedSerializer {
159183

160184
if let Some(ser_schema) = schema.get_as::<Bound<'_, PyDict>>(intern!(py, "serialization"))? {
161185
let op_ser_type: Option<Bound<'_, PyString>> = ser_schema.get_as(type_key)?;
162-
match op_ser_type.as_ref().map(|py_str| py_str.to_str()).transpose()? {
163-
Some("function-plain") => {
164-
// `function-plain` is a special case, not included in `find_serializer` since it means
186+
let op_ser_type = op_ser_type.as_ref().map(|py_str| py_str.to_str()).transpose()?;
187+
let new_type = op_ser_type.map(|typ| get_new_type(schema.py(), typ).unwrap());
188+
189+
match new_type.as_deref() {
190+
Some("serializer-function-plain") => {
191+
// `serializer-function-plain` is a special case, not included in `find_serializer` since it means
165192
// something different in `schema.type`
166193
// NOTE! we use the `schema` here, not `ser_schema`
167194
return super::type_serializers::function::FunctionPlainSerializer::build(
168195
schema,
169196
config,
170197
definitions,
171198
)
172-
.map_err(|err| py_schema_error_type!("Error building `function-plain` serializer:\n {}", err));
199+
.map_err(|err| {
200+
py_schema_error_type!("Error building `serializer-function-plain` serializer:\n {}", err)
201+
});
173202
}
174-
Some("function-wrap") => {
175-
// `function-wrap` is also a special case, not included in `find_serializer` since it mean
203+
Some("serializer-function-wrap") => {
204+
// `serializer-function-wrap` is also a special case, not included in `find_serializer` since it mean
176205
// something different in `schema.type`
177206
// NOTE! we use the `schema` here, not `ser_schema`
178207
return super::type_serializers::function::FunctionWrapSerializer::build(
179208
schema,
180209
config,
181210
definitions,
182211
)
183-
.map_err(|err| py_schema_error_type!("Error building `function-wrap` serializer:\n {}", err));
212+
.map_err(|err| {
213+
py_schema_error_type!("Error building `serializer-function-wrap` serializer:\n {}", err)
214+
});
184215
}
185216
// applies to lists tuples and dicts, does not override the main schema `type`
186217
Some("include-exclude-sequence" | "include-exclude-dict") => (),

src/serializers/type_serializers/function.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl BuildSerializer for FunctionAfterSerializerBuilder {
6262
pub struct FunctionPlainSerializerBuilder;
6363

6464
impl BuildSerializer for FunctionPlainSerializerBuilder {
65-
const EXPECTED_TYPE: &'static str = "function-plain";
65+
const EXPECTED_TYPE: &'static str = "serializer-function-plain";
6666
fn build(
6767
schema: &Bound<'_, PyDict>,
6868
config: Option<&Bound<'_, PyDict>>,
@@ -95,7 +95,7 @@ fn destructure_function_schema<'py>(schema: &Bound<'py, PyDict>) -> PyResult<(bo
9595
}
9696

9797
impl BuildSerializer for FunctionPlainSerializer {
98-
const EXPECTED_TYPE: &'static str = "function-plain";
98+
const EXPECTED_TYPE: &'static str = "serializer-function-plain";
9999

100100
/// NOTE! `schema` here is the actual `CoreSchema`, not `schema.serialization` as in the other builders
101101
/// (done this way to match `FunctionWrapSerializer` which requires the full schema)
@@ -125,7 +125,7 @@ impl BuildSerializer for FunctionPlainSerializer {
125125
}
126126
};
127127

128-
let name = format!("plain_function[{function_name}]");
128+
let name = format!("serializer-function-plain[{function_name}]");
129129
Ok(Self {
130130
func: function.unbind(),
131131
function_name,
@@ -310,7 +310,7 @@ fn copy_outer_schema<'py>(schema: &Bound<'py, PyDict>) -> PyResult<Bound<'py, Py
310310
pub struct FunctionWrapSerializerBuilder;
311311

312312
impl BuildSerializer for FunctionWrapSerializerBuilder {
313-
const EXPECTED_TYPE: &'static str = "function-wrap";
313+
const EXPECTED_TYPE: &'static str = "serializer-function-wrap";
314314
fn build(
315315
schema: &Bound<'_, PyDict>,
316316
config: Option<&Bound<'_, PyDict>>,
@@ -339,7 +339,7 @@ pub struct FunctionWrapSerializer {
339339
}
340340

341341
impl BuildSerializer for FunctionWrapSerializer {
342-
const EXPECTED_TYPE: &'static str = "function-wrap";
342+
const EXPECTED_TYPE: &'static str = "serializer-function-wrap";
343343

344344
/// NOTE! `schema` here is the actual `CoreSchema`, not `schema.serialization` as in the other builders
345345
/// (done this way since we need the `CoreSchema`)
@@ -368,7 +368,7 @@ impl BuildSerializer for FunctionWrapSerializer {
368368
None => AnySerializer::build(schema, config, definitions)?,
369369
};
370370

371-
let name = format!("wrap_function[{function_name}, {}]", serializer.get_name());
371+
let name = format!("serializer-function-wrap[{function_name}, {}]", serializer.get_name());
372372
Ok(Self {
373373
serializer: Arc::new(serializer),
374374
func: function.into(),

src/validators/function.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub struct FunctionBeforeValidator {
8989
info_arg: bool,
9090
}
9191

92-
impl_build!(FunctionBeforeValidator, "function-before");
92+
impl_build!(FunctionBeforeValidator, "validator-function-before");
9393

9494
impl FunctionBeforeValidator {
9595
fn _validate<'s, 'py>(
@@ -157,7 +157,7 @@ pub struct FunctionAfterValidator {
157157
info_arg: bool,
158158
}
159159

160-
impl_build!(FunctionAfterValidator, "function-after");
160+
impl_build!(FunctionAfterValidator, "validator-function-after");
161161

162162
impl FunctionAfterValidator {
163163
fn _validate<'py, I: Input<'py> + ?Sized>(
@@ -225,7 +225,7 @@ pub struct FunctionPlainValidator {
225225
}
226226

227227
impl BuildValidator for FunctionPlainValidator {
228-
const EXPECTED_TYPE: &'static str = "function-plain";
228+
const EXPECTED_TYPE: &'static str = "validator-function-plain";
229229

230230
fn build(
231231
schema: &Bound<'_, PyDict>,
@@ -240,7 +240,10 @@ impl BuildValidator for FunctionPlainValidator {
240240
Some(c) => c.clone().into(),
241241
None => py.None(),
242242
},
243-
name: format!("function-plain[{}()]", function_name(function_info.function.bind(py))?),
243+
name: format!(
244+
"validator-function-plain[{}()]",
245+
function_name(function_info.function.bind(py))?
246+
),
244247
field_name: function_info.field_name.clone(),
245248
info_arg: function_info.info_arg,
246249
}
@@ -284,7 +287,7 @@ pub struct FunctionWrapValidator {
284287
}
285288

286289
impl BuildValidator for FunctionWrapValidator {
287-
const EXPECTED_TYPE: &'static str = "function-wrap";
290+
const EXPECTED_TYPE: &'static str = "validator-function-wrap";
288291

289292
fn build(
290293
schema: &Bound<'_, PyDict>,
@@ -303,7 +306,10 @@ impl BuildValidator for FunctionWrapValidator {
303306
Some(c) => c.clone().into(),
304307
None => py.None(),
305308
},
306-
name: format!("function-wrap[{}()]", function_name(function_info.function.bind(py))?),
309+
name: format!(
310+
"validator-function-wrap[{}()]",
311+
function_name(function_info.function.bind(py))?
312+
),
307313
field_name: function_info.field_name.clone(),
308314
info_arg: function_info.info_arg,
309315
hide_input_in_errors,

0 commit comments

Comments
 (0)