Skip to content

Commit cc3ac46

Browse files
authored
Mark constructor parameters of exceptions as positional-only (#1699)
1 parent bd24ed0 commit cc3ac46

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -714,22 +714,16 @@ 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.
717722
"""
718723

719724
def __init__(
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: ...
725+
self, error_type: LiteralString, message_template: LiteralString, context: dict[str, Any] | None = None, /
726+
) -> None: ...
733727
@property
734728
def context(self) -> dict[str, Any] | None:
735729
"""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):
757751
758752
def custom_validator(v) -> None:
759753
if v <= 10:
760-
raise PydanticKnownError(error_type='greater_than', context={'gt': 10})
754+
raise PydanticKnownError('greater_than', {'gt': 10})
761755
return v
762756
```
763-
"""
764-
765-
def __init__(self, error_type: ErrorType, context: dict[str, Any] | None = None) -> None:
766-
"""Initializes the `PydanticKnownError`.
767757
768-
Arguments:
769-
error_type: The error type.
770-
context: The data to inject into the message template.
771-
"""
758+
Arguments:
759+
error_type: The error type.
760+
context: The data to inject into the message template.
761+
"""
772762

773-
def __new__(cls, error_type: ErrorType, context: dict[str, Any] | None = None) -> Self: ...
763+
def __init__(self, error_type: ErrorType, context: dict[str, Any] | None = None, /) -> None: ...
774764
@property
775765
def context(self) -> dict[str, Any] | None:
776766
"""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):
870860
"""An error raised when an issue occurs during serialization.
871861
872862
In custom serializers, this error can be used to indicate that serialization has failed.
873-
"""
874-
875-
def __init__(self, message: str) -> None:
876-
"""Initializes the `PydanticSerializationError`.
877863
878-
Arguments:
879-
message: The message associated with the error.
880-
"""
864+
Arguments:
865+
message: The message associated with the error.
866+
"""
881867

882-
def __new__(cls, message: str) -> Self: ...
868+
def __init__(self, message: str, /) -> None: ...
883869

884870
@final
885871
class PydanticSerializationUnexpectedValue(ValueError):
@@ -918,16 +904,12 @@ class PydanticSerializationUnexpectedValue(ValueError):
918904
919905
This is often used internally in `pydantic-core` when unexpected types are encountered during serialization,
920906
but it can also be used by users in custom serializers, as seen above.
921-
"""
922-
923-
def __init__(self, message: str) -> None:
924-
"""Initializes the `PydanticSerializationUnexpectedValue`.
925907
926-
Arguments:
927-
message: The message associated with the unexpected value.
928-
"""
908+
Arguments:
909+
message: The message associated with the unexpected value.
910+
"""
929911

930-
def __new__(cls, message: str | None = None) -> Self: ...
912+
def __init__(self, message: str, /) -> None: ...
931913

932914
@final
933915
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl PydanticSerializationError {
8181
#[pymethods]
8282
impl PydanticSerializationError {
8383
#[new]
84+
#[pyo3(signature = (message, /))]
8485
fn py_new(message: String) -> Self {
8586
Self { message }
8687
}
@@ -139,7 +140,7 @@ impl PydanticSerializationUnexpectedValue {
139140
#[pymethods]
140141
impl PydanticSerializationUnexpectedValue {
141142
#[new]
142-
#[pyo3(signature = (message=None, field_type=None, input_value=None))]
143+
#[pyo3(signature = (message=None, field_type=None, input_value=None, /))]
143144
fn py_new(message: Option<String>, field_type: Option<String>, input_value: Option<PyObject>) -> Self {
144145
Self {
145146
message,

0 commit comments

Comments
 (0)