-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Pydantic Helper Functions Raise json.JSONDecodeError #4985
Comments
Digging into the implementation, it seems that |
It's returning You will get a >>> pydantic.parse_raw_as(int, "{}")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/hasan/work/pydantic/pydantic/tools.py", line 82, in parse_raw_as
return parse_obj_as(type_, obj, type_name=type_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/hasan/work/pydantic/pydantic/tools.py", line 38, in parse_obj_as
return model_type(__root__=obj).__root__
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/hasan/work/pydantic/pydantic/main.py", line 342, in __init__
raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for ParsingModel[int]
__root__
value is not a valid integer (type=type_error.integer) I think we can close this issue because or wrap string conversion error with What do you think @samuelcolvin |
This behavior though, is different than the behaviour with normal model fields. If you pass Would be good to keep the behaviour consistent. |
Are you sure that you get |
Sure, thinking of the following example:
Same json.JSONDecodeError, but it's wrapped by a pydantic.ValidationError, which is more convenient for exception handling. |
Confirmed, thanks for reporting. Question is whether fixing this in V1.10 would qualify as a breaking change? I think it might. it'll definitely be fixed in V2 as all JSON parsing happens in pydantic-core, and |
Thanks @samuelcolvin — that's cool to hear about V2! I think it would be good to update this in V1.10—anyone explicitly catching Happy to provide the PR if you agree. |
Well, you don't need to catch More specifcally you can catch each separately. I'll see what others say, I guess I'm neutral, on adding a fix to V2, virtually anything can be a breaking that at this point. |
Thanks @dhruvrajan for clearing this up. |
All makes sense!—That's a good tip, I'll use Looking forward to the V2 release! Feel free to close the issue. |
It seems to me this issue has been resolved in v2 so I'm going to close this now. If you notice related issues in v2 we'd appreciate the reports! |
Initial Checks
Description
The error handling docs provide that when an issue is encountered when validating data, a
pydantic.ValidationError
will be thrown.However there are cases when a json.JSONDecodeError is thrown. I'm including an example below, (
'True'
is an invalid boolean in JSON;'true'
is valid).Raising this issue to suggest that either the error be wrapped by a
ValidationError
or the docs be updated.Thanks for the hard work!
Example Code
Python, Pydantic & OS Version
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: