Skip to content

Mark constructor parameters of exceptions as positional-only #1699

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 21 additions & 39 deletions python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/errors/value_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bound<'_, PyDict>>) -> Self {
Self {
error_type,
Expand Down Expand Up @@ -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<Bound<'_, PyDict>>) -> PyResult<Self> {
let error_type = ErrorType::new(py, error_type, context)?;
Ok(Self { error_type })
Expand Down
3 changes: 2 additions & 1 deletion src/serializers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl PydanticSerializationError {
#[pymethods]
impl PydanticSerializationError {
#[new]
#[pyo3(signature = (message, /))]
fn py_new(message: String) -> Self {
Self { message }
}
Expand Down Expand Up @@ -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<String>, field_type: Option<String>, input_value: Option<PyObject>) -> Self {
Self {
message,
Expand Down
Loading