You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm hoping to incorporate some custom error handling using the :class:PydanticKnownError. I tried to use the example from the docs, but this raises a TypeError rather than the expected PydanticKnownError.
A potentially quick solution would be to mark the arguments as positional-only with / and updating the docs to reflect this, but this appears to be the opposite of most pydantic classes.
Example Code
# from the example docs -> https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.PydanticKnownErrorfrompydantic_coreimportPydanticKnownErrordefcustom_validator(v) ->None:
ifv<=10:
raisePydanticKnownError(error_type='greater_than', context={'gt': 10})
returnvcustom_validator(10)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 custom_validator(10)
Cell In[3], line 5, in custom_validator(v)
3defcustom_validator(v) -> None:
4if v <=10:
----> 5 raise PydanticKnownError(error_type='greater_than', context={'gt': 10})
6return v
TypeError: pydantic_core._pydantic_core.PydanticKnownError() takes no keyword arguments
The bug can be replicated for PydanticCustomError .
frompydantic_coreimportPydanticCustomErrordefcustom_validator(v) ->None:
ifv<=10:
raisePydanticCustomError(error_type='custom_value_error', message_template='Value must be greater than {value}', context={'value': 10, 'extra_context': 'extra_data'})
returnvcustom_validator(10)
The example in the documentation though only uses positional arguments.
A quick investigation seems to suggest that no class that extends PyValueError in pydantic core can take keyword arguments regardless of signature. I'm not sure whether this is a bug in or a feature of PyO3.
Initial Checks
Description
I'm hoping to incorporate some custom error handling using the :class:
PydanticKnownError
. I tried to use the example from the docs, but this raises aTypeError
rather than the expectedPydanticKnownError
.A potentially quick solution would be to mark the arguments as positional-only with
/
and updating the docs to reflect this, but this appears to be the opposite of mostpydantic
classes.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: