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
It seems that since the latest FastAPI releases (when pydantic V2 became supported, which is fastapi==0.100 onwards), freezegun has stopped working with certain FastAPI features.
Upon creating a FastAPI endpoint with pydantic models which use datetime and possibly other related date types, where freezegun has been activated - the application fails due to a schema generation failure. The failure can also occur if you instantiate the app first, then activate freezegun, and make a request after (e.g. when running a test suite with an ASGI client)
There is a somewhat related discussion over in Pydantic about freezegun and pydantic being incompatible, though the fix mentioned doesn't seem to work for a FastAPI pytest case (the author has fixed it for a django pytest suite). I have also tried to patch the associated pydantic schema methods to no avail.
I thought it would be best to put the issue here, to determine how best to work around pydantic v2 with freezegun, as it seems that the patching that freezegun is doing may be incompatible with Pydantic.
Example
This is a simple script example to demonstrate the failure (a more realistic example would be a pytest test which is wrapped in a freezegun context and is calling fastAPI via some test ASGI client)
import datetime
import freezegun
from pydantic import BaseModel, ConfigDict
class Model(BaseModel):
field_1: datetime.datetime
model_config = ConfigDict(arbitrary_types_allowed=True)
with freezegun.freeze_time("2021-05-04T03:04:17"):
from fastapi import FastAPI, Depends
app = FastAPI()
@app.get("/data")
async def get(mm: Model = Depends(Model)) -> None:
print(3)
return None
Running this script causes the following failure:
Traceback (most recent call last):
File "/home/vivanov/exp/apps/test_api/schema_2.py", line 20, in <module>
@app.get("/data")
^^^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/routing.py", line 944, in decorator
self.add_api_route(
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/routing.py", line 883, in add_api_route
route = route_class(
^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/routing.py", line 513, in __init__
self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 268, in get_dependant
sub_dependant = get_param_sub_dependant(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 111, in get_param_sub_dependant
return get_sub_dependant(
^^^^^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 147, in get_sub_dependant
sub_dependant = get_dependant(
^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 261, in get_dependant
type_annotation, depends, param_field = analyze_param(
^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 444, in analyze_param
field = create_response_field(
^^^^^^^^^^^^^^^^^^^^^^
File "/home/vivanov/.virtualenvs/venv-test/lib/python3.11/site-packages/fastapi/utils.py", line 101, in create_response_field
raise fastapi.exceptions.FastAPIError(
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'datetime.datetime'> is a valid Pydantic field type.
If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None.
Read more: https://fastapi.tiangolo.com/tutorial/response-model/
The fastapi.exceptions.FastAPIError is raised from an underlying Pydantic exception which is more illuminating:
PydanticSchemaGenerationError("Unable to generate pydantic-core schema for <class 'datetime.datetime'>.
Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.\n\n
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.")
It seems that since the latest FastAPI releases (when pydantic V2 became supported, which is fastapi==0.100 onwards), freezegun has stopped working with certain FastAPI features.
Upon creating a FastAPI endpoint with pydantic models which use
datetime
and possibly other related date types, where freezegun has been activated - the application fails due to a schema generation failure. The failure can also occur if you instantiate the app first, then activate freezegun, and make a request after (e.g. when running a test suite with an ASGI client)There is a somewhat related discussion over in Pydantic about freezegun and pydantic being incompatible, though the fix mentioned doesn't seem to work for a FastAPI pytest case (the author has fixed it for a django pytest suite). I have also tried to patch the associated pydantic schema methods to no avail.
I thought it would be best to put the issue here, to determine how best to work around pydantic v2 with freezegun, as it seems that the patching that freezegun is doing may be incompatible with Pydantic.
Example
This is a simple script example to demonstrate the failure (a more realistic example would be a pytest test which is wrapped in a freezegun context and is calling fastAPI via some test ASGI client)
Running this script causes the following failure:
The
fastapi.exceptions.FastAPIError
is raised from an underlying Pydantic exception which is more illuminating:Dev environment
To replicate the above failure:
Cpython: 3.11.8
Ubuntu: 22.04.4 LTS
Kernel: 6.5.0-41-generic
Python reqs:
(NOTE
Can confirm that it works with the last fastAPI version prior to pydantic v2 support - 0.99.0
)
The text was updated successfully, but these errors were encountered: