-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
🦺 Update implementation and docs for RequestValidationError in Pydantic v2
#11542
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
Conversation
45a560a to
338a33e
Compare
|
@tiangolo this is green now. I believe it is technically a breaking change. |
025ef5f to
b91428d
Compare
This comment was marked as resolved.
This comment was marked as resolved.
|
3 months and no review :( |
|
Dang, I just checked out your PR and tried it for my test. It doesn't fix it because I think this code block still needs to strip out perhaps See here in pydantic docs: https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.ValidationError.errors |
|
Hi @tamird 👋 We'll be reviewing this PR soon. 🤓 Thank you for your interest and time in helping us with your contribution 🙇 |
3f38cf3 to
39429c1
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@tamird thanks for the interest. There's no need to ping or contact individual maintainers, we already have this PR in the project and we'll review it eventually. If you would like to help accelerate things, there are many ways to contribute, for example helping others with questions: https://fastapi.tiangolo.com/help-fastapi/#help-others-with-questions-in-github |
39429c1 to
9b65f64
Compare
YuriiMotov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR modifies ValidationException and derived classes (RequestValidationError and ResponseValidationError) so that their .errors() method would return Sequence["ErrorDetails"] instead of Sequence[Any].
Where ErrorDetails is a TypedDict imported from Pydantic (pydantic.error_wrappers.ErrorDict for V1 and pydantic_core.ErrorDetails for V2).
class ErrorDetails(_TypedDict):
type: str
loc: tuple[int | str, ...]
msg: str
input: _Any
ctx: _NotRequired[dict[str, _Any]]
url: _NotRequired[str]So, I guess the intention is to provide type hints in case user write something like:
from fastapi import Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
async def request_validation_exception_handler(
request: Request, exc: RequestValidationError
) -> JSONResponse:
errors = exc.errors() # Here I expect to ge list of `ErrorDetails`
...But in VSCode I can still see that elements of errors are of type Any:

@tamird, could you please take a look?
I do not see the token "Any" in your screenshot. |
I looked at the code from this comment. And I use fresh virtual environment with the latest versions of packages installed using I don't think this is a problem of my environment set up. If I create my own function with the same return type annotation, it works well And if I try the same with: from fastapi.exceptions import ValidationException
exc = ValidationException([])
b = exc.errors()[0]It gives my Full code example that you or anybody else can try: from collections.abc import Sequence
from fastapi.exceptions import ValidationException
from pydantic_core import ErrorDetails
# =============================================
def my_errors() -> Sequence["ErrorDetails"]:
pass
a = my_errors()[0] # ErrorDetails
# =============================================
exc = ValidationException([])
b = exc.errors()[0] # Any
Sorry, I didn't get the above messages. Could you please clarify what you mean? |
These tests are all marked `@needs_pydanticv2` so they don't run on Pydantic V1.
b8cba06 to
d317859
Compare
|
@YuriiMotov I have added type annotations in the relevant tests and confirmed locally that changing the annotations to |
This will allow mypy to check the return type of `ValidationException.errors()` in a later commit.
49b0e39 to
7ad235e
Compare
Change the signature of `ValidationException.errors` to return `Sequence[pydantic_core.ErrorDetails]` in Pydantic V2. Add type annotations in tests and locally run mypy to ensure it fails if the annotations changes to e.g. `Sequence[str]`. Unfortunately running mypy over tests isn't currently part of CI.
7ad235e to
ee3c9ea
Compare
|
This still doesn't show me type hints in VSCode for the following code example with Pydantic V2: from fastapi.exceptions import ValidationException
exc = ValidationException([])
b = exc.errors()[0] # VSCode shows `(variable) b: Any`At the same time it does show type correctly with Pydantic V1. I think we should either make it working with Pydantic V2 or explain what is the purpose of this PR (what it gives us) |
I'd prefer not to try to debug your VSCode setup. If you run |
I think it's not a problem of my VSCode setup. It works perfectly fine in other cases. |
|
It looks like the Let me play with it some more. |
|
This pull request has a merge conflict that needs to be resolved. |




Change the signature of
ValidationException.errorsto returnSequence[pydantic_core.ErrorDetails]in Pydantic V2.Update the documentation to explain that
RequestValidationErrorisn'tliterally a subclass since Pydantic V2.
Closes (UPD YuriiMotov: actually not) #10424.
Closes #11176.