Skip to content

Commit 3a55559

Browse files
committed
move error message
1 parent a896b4c commit 3a55559

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

python/pydantic_core/core_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,6 +4122,7 @@ def never_schema(
41224122
'complex_type',
41234123
'complex_str_parsing',
41244124
'never',
4125+
'never_serializing',
41254126
]
41264127

41274128

src/errors/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ error_types! {
431431
ComplexType {},
432432
ComplexStrParsing {},
433433
Never {},
434+
NeverSerializing {},
434435
}
435436

436437
macro_rules! render {
@@ -577,7 +578,8 @@ impl ErrorType {
577578
Self::DecimalWholeDigits {..} => "Decimal input should have no more than {whole_digits} digit{expected_plural} before the decimal point",
578579
Self::ComplexType {..} => "Input should be a valid python complex object, a number, or a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex",
579580
Self::ComplexStrParsing {..} => "Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex",
580-
Self::Never { .. } => "No input is allowed for `typing.Never`"
581+
Self::Never { .. } => "No input is allowed for `typing.Never`",
582+
Self::NeverSerializing { .. } => "Type `typing.Never` cannot be serialized"
581583
}
582584
}
583585

src/serializers/type_serializers/never.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use super::{py_err_se_err, BuildSerializer, CombinedSerializer, Extra, TypeSerializer};
22
use crate::definitions::DefinitionsBuilder;
3+
use crate::errors::ErrorTypeDefaults;
34
use crate::tools::py_err;
45
use pyo3::exceptions::PyTypeError;
56
use pyo3::prelude::*;
67
use pyo3::types::PyDict;
78
use std::borrow::Cow;
89

9-
const ERROR_MESSAGE: &str = "type `never` cannot be serialized";
10-
1110
#[derive(Debug)]
1211
pub struct NeverSerializer;
1312

@@ -33,11 +32,11 @@ impl TypeSerializer for NeverSerializer {
3332
_exclude: Option<&Bound<'_, PyAny>>,
3433
_extra: &Extra,
3534
) -> PyResult<PyObject> {
36-
py_err!(PyTypeError; ERROR_MESSAGE)
35+
py_err!(PyTypeError; ErrorTypeDefaults::Never.message_template_python())
3736
}
3837

3938
fn json_key<'a>(&self, _key: &'a Bound<'_, PyAny>, _extra: &Extra) -> PyResult<Cow<'a, str>> {
40-
py_err!(PyTypeError; ERROR_MESSAGE)
39+
py_err!(PyTypeError; ErrorTypeDefaults::Never.message_template_python())
4140
}
4241

4342
fn serde_serialize<S: serde::ser::Serializer>(
@@ -48,7 +47,7 @@ impl TypeSerializer for NeverSerializer {
4847
_exclude: Option<&Bound<'_, PyAny>>,
4948
_extra: &Extra,
5049
) -> Result<S::Ok, S::Error> {
51-
py_err!(PyTypeError; ERROR_MESSAGE).map_err(py_err_se_err)
50+
py_err!(PyTypeError; ErrorTypeDefaults::Never.message_template_python()).map_err(py_err_se_err)
5251
}
5352

5453
fn get_name(&self) -> &str {

tests/test_errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ def f(input_value, info):
411411
None,
412412
),
413413
('never', 'No input is allowed for `typing.Never`', None),
414+
('never_serializing', 'Type `typing.Never` cannot be serialized', None),
414415
]
415416

416417

0 commit comments

Comments
 (0)