-
Notifications
You must be signed in to change notification settings - Fork 274
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
Freeze time doesn't work with FastAPI + pydantic V2 #551
Comments
I'm running into this problem too. @Vivanov98 did you find a solution by chance? |
I've run into a similar issue. It seems like Pydantic does not work well with the datetime type. |
Same issue here. Switched to https://github.com/adamchainz/time-machine |
I had a similar issue and found two solutions that worked for me, they just have their tradeoffs. Option 1Monkey patch
Option 1 Downsides:It is a monkey patch on a potentially volatile class. Pydantic docs say
and if you try to even subclass GenerateSchema today, you get a warning
Option 2Pydantic docs have a section on handling third-party types that worked for me. Applying it to the example on this issue it looks like
Since Pydantic respects Annotated, (and the underlying issue is that Pydantic doesn't have a built-in schema for freezegun's datetime and freezegun doesn't provide one for Pydantic) we can intervene by providing the mapping ourselves. You can also do the same thing for Option 2 Downsides:You'll have to annotate every datetime (or date) that you want to use with freezegun. It is definitely test-induced design damage, but ended up being the best escape hatch for me. Python 3.9 requirements (partial):
|
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: