Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Prepare for Pydantic V2 release #5806

Closed
9 tasks done
kigawas opened this issue Dec 21, 2022 · 2 comments
Closed
9 tasks done

Prepare for Pydantic V2 release #5806

kigawas opened this issue Dec 21, 2022 · 2 comments

Comments

@kigawas
Copy link

kigawas commented Dec 21, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# any fastapi code is OK

from fastapi import FastAPI

app = FastAPI()


@app.get("")
async def main():
    return "ok"

Description

Currently, the FastAPI's internal Pydantic models are not compatible with Pydantic V2.

For example,

  • dependencies/models.py
  • openapi/models.py
  • exceptions.py

As Pydantic V2 is implemented with Rust, it should be quite performant than V1. I suggest preparing for its V2 in advance so that FastAPI can take advantage of it.

Wanted Solution

We have to solve lots of errors. There are several categories:

  • Errors from parameter change, for example: Field(gte=0) becomes Field(ge=0)
  • Errors from Pydantic implementation change, you may see some recursion errors

Wanted Code

# user should not rewrite their FastAPI code, or at least there is little to rewrite

from fastapi import FastAPI

app = FastAPI()


@app.get("")
async def main():
    return "ok"

Alternatives

No response

Operating System

Linux, Windows, macOS, Other

Operating System Details

No response

FastAPI Version

0.88.0

Python Version

3.11.0

Additional Context

I'll give two typical errors here. When updated to Pydantic V2 and applied some parameter changes, if you

  • import fastapi, you'll see:
  File "/Users/myfastapi/.venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 88, in generate_schema
    return get_schema(types_namespace=self.types_namespace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/myfastapi/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 103, in deferred_model_get_pydantic_validation_schema
    inner_schema, fields = build_inner_schema(
                           ^^^^^^^^^^^^^^^^^^^
  File "/Users/myfastapi/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 189, in build_inner_schema
    self_schema = core_schema.new_class_schema(cls, core_schema.recursive_reference_schema(model_ref))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/myfastapi/.venv/lib/python3.11/site-packages/pydantic_core/core_schema.py", line 901, in new_class_schema
    return dict_not_none(
           ^^^^^^^^^^^^^^
  File "/Users/myfastapi/.venv/lib/python3.11/site-packages/pydantic_core/core_schema.py", line 19, in dict_not_none
    return {k: v for k, v in kwargs.items() if v is not None}
                             ^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while calling a Python object
  • start fastapi with uvicorn, you'll see
  File "/usr/local/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 2183, in __class_getitem__
    return _AnnotatedAlias(origin, metadata)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 2090, in __init__
    super().__init__(origin, origin)
  File "/usr/local/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1338, in __init__
    super().__init__(origin, inst=inst, name=name)
  File "/usr/local/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1242, in __init__
    self._inst = inst
    ^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1281, in __setattr__
    if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams',
       ^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/python@3.11/3.11.0/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1230, in _is_dunder
    return attr.startswith('__') and attr.endswith('__')
           ^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while calling a Python object
@kigawas kigawas added the feature New feature or request label Dec 21, 2022
@iudeen
Copy link
Contributor

iudeen commented Dec 21, 2022

I think this is already planned. Samuel(Pydantic Maintainer) is also a frequent contributor to FastAPI.

Once V2 is released officially, the compatibility should be ported.

@yezz123
Copy link
Contributor

yezz123 commented Dec 21, 2022

The migration to V2 I guess it's not gonna be the major thing cause FastAPI until now gonna be stable and working on providing a new version each time there is progress in making the framework more Fast.

so the point here is FastAPI will migrate to Pydantic 2 but not now still need time and also a bunch of surveys to see people who use the latest versions and do updates to their packages, while maybe it will touch the way how some projects have customized classes and models in pydantic, lets wait and see

@tiangolo tiangolo added question Question or problem reviewed question-migrate and removed feature New feature or request labels Feb 24, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 24, 2023
@tiangolo tiangolo converted this issue into discussion #6051 Feb 24, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants