Skip to content

Commit b848976

Browse files
authored
Revert "Mark constructor parameters of exceptions as positional-only (#1699)"
This reverts commit cc3ac46.
1 parent cc3ac46 commit b848976

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -714,16 +714,22 @@ class PydanticCustomError(ValueError):
714714
raise PydanticCustomError('custom_value_error', 'Value must be greater than {value}', {'value': 10, 'extra_context': 'extra_data'})
715715
return v
716716
```
717-
718-
Arguments:
719-
error_type: The error type.
720-
message_template: The message template.
721-
context: The data to inject into the message template.
722717
"""
723718

724719
def __init__(
725-
self, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None, /
726-
) -> None: ...
720+
self, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None
721+
) -> None:
722+
"""Initializes the `PydanticCustomError`.
723+
724+
Arguments:
725+
error_type: The error type.
726+
message_template: The message template.
727+
context: The data to inject into the message template.
728+
"""
729+
730+
def __new__(
731+
cls, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None
732+
) -> Self: ...
727733
@property
728734
def context(self) -> dict[str, Any] | None:
729735
"""Values which are required to render the error message, and could hence be useful in passing error data forward."""
@@ -751,16 +757,20 @@ class PydanticKnownError(ValueError):
751757
752758
def custom_validator(v) -> None:
753759
if v <= 10:
754-
raise PydanticKnownError('greater_than', {'gt': 10})
760+
raise PydanticKnownError(error_type='greater_than', context={'gt': 10})
755761
return v
756762
```
757-
758-
Arguments:
759-
error_type: The error type.
760-
context: The data to inject into the message template.
761763
"""
762764

763-
def __init__(self, error_type: ErrorType, context: dict[str, Any] | None = None, /) -> None: ...
765+
def __init__(self, error_type: ErrorType, context: dict[str, Any] | None = None) -> None:
766+
"""Initializes the `PydanticKnownError`.
767+
768+
Arguments:
769+
error_type: The error type.
770+
context: The data to inject into the message template.
771+
"""
772+
773+
def __new__(cls, error_type: ErrorType, context: dict[str, Any] | None = None) -> Self: ...
764774
@property
765775
def context(self) -> dict[str, Any] | None:
766776
"""Values which are required to render the error message, and could hence be useful in passing error data forward."""
@@ -860,12 +870,16 @@ class PydanticSerializationError(ValueError):
860870
"""An error raised when an issue occurs during serialization.
861871
862872
In custom serializers, this error can be used to indicate that serialization has failed.
863-
864-
Arguments:
865-
message: The message associated with the error.
866873
"""
867874

868-
def __init__(self, message: str, /) -> None: ...
875+
def __init__(self, message: str) -> None:
876+
"""Initializes the `PydanticSerializationError`.
877+
878+
Arguments:
879+
message: The message associated with the error.
880+
"""
881+
882+
def __new__(cls, message: str) -> Self: ...
869883

870884
@final
871885
class PydanticSerializationUnexpectedValue(ValueError):
@@ -904,12 +918,16 @@ class PydanticSerializationUnexpectedValue(ValueError):
904918
905919
This is often used internally in `pydantic-core` when unexpected types are encountered during serialization,
906920
but it can also be used by users in custom serializers, as seen above.
907-
908-
Arguments:
909-
message: The message associated with the unexpected value.
910921
"""
911922

912-
def __init__(self, message: str, /) -> None: ...
923+
def __init__(self, message: str) -> None:
924+
"""Initializes the `PydanticSerializationUnexpectedValue`.
925+
926+
Arguments:
927+
message: The message associated with the unexpected value.
928+
"""
929+
930+
def __new__(cls, message: str | None = None) -> Self: ...
913931

914932
@final
915933
class ArgsKwargs:

src/errors/value_exception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct PydanticCustomError {
6565
#[pymethods]
6666
impl PydanticCustomError {
6767
#[new]
68-
#[pyo3(signature = (error_type, message_template, context = None, /))]
68+
#[pyo3(signature = (error_type, message_template, context = None))]
6969
pub fn py_new(error_type: String, message_template: String, context: Option<Bound<'_, PyDict>>) -> Self {
7070
Self {
7171
error_type,
@@ -144,7 +144,7 @@ pub struct PydanticKnownError {
144144
#[pymethods]
145145
impl PydanticKnownError {
146146
#[new]
147-
#[pyo3(signature = (error_type, context=None, /))]
147+
#[pyo3(signature = (error_type, context=None))]
148148
pub fn py_new(py: Python, error_type: &str, context: Option<Bound<'_, PyDict>>) -> PyResult<Self> {
149149
let error_type = ErrorType::new(py, error_type, context)?;
150150
Ok(Self { error_type })

src/serializers/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ impl PydanticSerializationError {
8181
#[pymethods]
8282
impl PydanticSerializationError {
8383
#[new]
84-
#[pyo3(signature = (message, /))]
8584
fn py_new(message: String) -> Self {
8685
Self { message }
8786
}
@@ -140,7 +139,7 @@ impl PydanticSerializationUnexpectedValue {
140139
#[pymethods]
141140
impl PydanticSerializationUnexpectedValue {
142141
#[new]
143-
#[pyo3(signature = (message=None, field_type=None, input_value=None, /))]
142+
#[pyo3(signature = (message=None, field_type=None, input_value=None))]
144143
fn py_new(message: Option<String>, field_type: Option<String>, input_value: Option<PyObject>) -> Self {
145144
Self {
146145
message,

0 commit comments

Comments
 (0)