-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
raise Err() from e and e.add_note() lose information in validation errors #6498
Comments
This comment is based on Pydantic V1 behaviorWith Pydantic it's slightly different by design. Validation errors in Pydantic aren't fatal, the process doesn't crash. Errors are being collected and are ready to be presented to a user. Important, the information is not lost. In the try:
assert v > 5, "Must be greater than 5"
except AssertionError as e:
raise ValueError(f"OUTER EXC!: {e}") In the Please, note that all of the above relates to a string representation of the try:
Foo(foo=4, bar=4)
except ValidationError as e:
for inner_error in e.raw_errors:
print(f'{inner_error} cause: {inner_error.exc.__cause__}')
print(f'{inner_error} notes: {inner_error.exc.__notes__}') Update: the comment was misleading |
raise Err() from e
and e.add_note()
lose information in validation errorsValidationError
string representation with __cause__
and __notes__
data
Apologies, accidentally closed, reopened. I'd disagree on a few points here:
try:
Foo(foo=4, bar=4)
except ValidationError as e:
for inner_error in e.errors():
pprint.pprint(inner_error, indent=2)
|
@zakstucke it could be that I was looking into Let me reproduce the issues you've reported and get back to you. |
ValidationError
string representation with __cause__
and __notes__
data
@zakstucke You're right 😇. In Pydantic V2 in Ideally, I would like to have an actual error object stored there. |
Here is a test illustrating the feature request https://github.com/pydantic/pydantic/pull/6504/files#diff-6e5030d96839d3fe377cf209b6cab5b0d50ae900b458248d8c24b61774117170R34 |
@zakstucke Thanks you for the request. I cannot say when we will be able to implement this in Pydantic V2. This will definitely involve improving error handling in |
No worries, thanks! |
@adriangb now that pydantic/pydantic-core#753 has been merged in, the exception info can now be accessed externally: from pydantic import BaseModel, field_validator, ValidationError
def check(v: int):
if v < 0:
raise ValueError("INNER ERR")
return v
class Foo(BaseModel):
foo: int
@field_validator("foo")
def check_foo(cls, v):
if v < 0:
try:
check(v)
except ValueError as e:
new_err = ValueError("OUTER ERR")
new_err.add_note("NOTE")
raise new_err from e
return v
try:
Foo(foo=-1)
except ValidationError as e:
for error in e.errors():
print(error["ctx"]["error"])
print(error["ctx"]["error"].__notes__)
print(error["ctx"]["error"].__cause__)
raise e Which prints:
Awesome thanks for that! However the traceback still formats as:
This was one of the most annoying things about V1, as it made debugging layers of errors so much harder than it usually is in native Python. Is there a specific reason to not allow showing these information chains (specifically enabled by the dev with |
I suppose it could be implemented |
@adriangb check out pydantic/pydantic-core#780 for a draft that would solve this |
Initial Checks
Description
Edit:
2
is actually a regression from v1, in 1.10.11 the outputted error wasMust be greater than 5 (type=assertion_error; __notes__=['ERR NOTE!'])
.Errors in V2 are on another level to V1, but error manipulation still loses information in the final
ValidationError
outputted by pydantic when:from e
syntaxe.add_note()
In both cases information is lost, which can be very useful when complex multi-func validation is going on to add context to inner errors at the top level.
I don't think either are a security concern being enabled by default in pydantic, both are opt in with
from e
andadd_note
respectively.Results in
Native errors when outside pydantic:
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeSelected Assignee: @lig
The text was updated successfully, but these errors were encountered: