diff --git a/python/pydantic_core/_pydantic_core.pyi b/python/pydantic_core/_pydantic_core.pyi index 17098cca5..398020bb1 100644 --- a/python/pydantic_core/_pydantic_core.pyi +++ b/python/pydantic_core/_pydantic_core.pyi @@ -714,22 +714,16 @@ class PydanticCustomError(ValueError): raise PydanticCustomError('custom_value_error', 'Value must be greater than {value}', {'value': 10, 'extra_context': 'extra_data'}) return v ``` + + Arguments: + error_type: The error type. + message_template: The message template. + context: The data to inject into the message template. """ def __init__( - self, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None - ) -> None: - """Initializes the `PydanticCustomError`. - - Arguments: - error_type: The error type. - message_template: The message template. - context: The data to inject into the message template. - """ - - def __new__( - cls, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None - ) -> Self: ... + self, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None, / + ) -> None: ... @property def context(self) -> dict[str, Any] | None: """Values which are required to render the error message, and could hence be useful in passing error data forward.""" @@ -757,20 +751,16 @@ class PydanticKnownError(ValueError): def custom_validator(v) -> None: if v <= 10: - raise PydanticKnownError(error_type='greater_than', context={'gt': 10}) + raise PydanticKnownError('greater_than', {'gt': 10}) return v ``` - """ - - def __init__(self, error_type: ErrorType, context: dict[str, Any] | None = None) -> None: - """Initializes the `PydanticKnownError`. - Arguments: - error_type: The error type. - context: The data to inject into the message template. - """ + Arguments: + error_type: The error type. + context: The data to inject into the message template. + """ - def __new__(cls, error_type: ErrorType, context: dict[str, Any] | None = None) -> Self: ... + def __init__(self, error_type: ErrorType, context: dict[str, Any] | None = None, /) -> None: ... @property def context(self) -> dict[str, Any] | None: """Values which are required to render the error message, and could hence be useful in passing error data forward.""" @@ -870,16 +860,12 @@ class PydanticSerializationError(ValueError): """An error raised when an issue occurs during serialization. In custom serializers, this error can be used to indicate that serialization has failed. - """ - - def __init__(self, message: str) -> None: - """Initializes the `PydanticSerializationError`. - Arguments: - message: The message associated with the error. - """ + Arguments: + message: The message associated with the error. + """ - def __new__(cls, message: str) -> Self: ... + def __init__(self, message: str, /) -> None: ... @final class PydanticSerializationUnexpectedValue(ValueError): @@ -918,16 +904,12 @@ class PydanticSerializationUnexpectedValue(ValueError): This is often used internally in `pydantic-core` when unexpected types are encountered during serialization, but it can also be used by users in custom serializers, as seen above. - """ - - def __init__(self, message: str) -> None: - """Initializes the `PydanticSerializationUnexpectedValue`. - Arguments: - message: The message associated with the unexpected value. - """ + Arguments: + message: The message associated with the unexpected value. + """ - def __new__(cls, message: str | None = None) -> Self: ... + def __init__(self, message: str, /) -> None: ... @final class ArgsKwargs: diff --git a/src/errors/value_exception.rs b/src/errors/value_exception.rs index d428dc524..964899905 100644 --- a/src/errors/value_exception.rs +++ b/src/errors/value_exception.rs @@ -65,7 +65,7 @@ pub struct PydanticCustomError { #[pymethods] impl PydanticCustomError { #[new] - #[pyo3(signature = (error_type, message_template, context = None))] + #[pyo3(signature = (error_type, message_template, context = None, /))] pub fn py_new(error_type: String, message_template: String, context: Option>) -> Self { Self { error_type, @@ -144,7 +144,7 @@ pub struct PydanticKnownError { #[pymethods] impl PydanticKnownError { #[new] - #[pyo3(signature = (error_type, context=None))] + #[pyo3(signature = (error_type, context=None, /))] pub fn py_new(py: Python, error_type: &str, context: Option>) -> PyResult { let error_type = ErrorType::new(py, error_type, context)?; Ok(Self { error_type }) diff --git a/src/serializers/errors.rs b/src/serializers/errors.rs index d9ba85682..c309b9e3a 100644 --- a/src/serializers/errors.rs +++ b/src/serializers/errors.rs @@ -81,6 +81,7 @@ impl PydanticSerializationError { #[pymethods] impl PydanticSerializationError { #[new] + #[pyo3(signature = (message, /))] fn py_new(message: String) -> Self { Self { message } } @@ -139,7 +140,7 @@ impl PydanticSerializationUnexpectedValue { #[pymethods] impl PydanticSerializationUnexpectedValue { #[new] - #[pyo3(signature = (message=None, field_type=None, input_value=None))] + #[pyo3(signature = (message=None, field_type=None, input_value=None, /))] fn py_new(message: Option, field_type: Option, input_value: Option) -> Self { Self { message,