DeprecationWarning: on_event is deprecated, use lifespan event handlers instead

220 views Asked by At

I was following the instructions in: https://testdriven.io/courses/tdd-fastapi/pytest-setup/

my requirements file is:

fastapi==0.109.0
pydantic-settings==2.1.0
uvicorn==0.26.0
asyncpg==0.29.0
tortoise-orm==0.20.0
httpx==0.26.0
pytest==7.4.4

I'm getting the error:

docker-compose exec web python -m pytest

============================================ test session starts =============================================
platform linux -- Python 3.12.1, pytest-7.4.4, pluggy-1.4.0
rootdir: /usr/src/app
plugins: anyio-4.3.0
collected 1 item

tests/test_ping.py .                                                                                   [100%]

============================================== warnings summary ==============================================
../../local/lib/python3.12/site-packages/tortoise/contrib/fastapi/__init__.py:92
../../local/lib/python3.12/site-packages/tortoise/contrib/fastapi/__init__.py:92
  /usr/local/lib/python3.12/site-packages/tortoise/contrib/fastapi/__init__.py:92: DeprecationWarning:
          on_event is deprecated, use lifespan event handlers instead.

          Read more about it in the
          [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).

    @app.on_event("startup")

../../local/lib/python3.12/site-packages/fastapi/applications.py:4495
../../local/lib/python3.12/site-packages/fastapi/applications.py:4495
../../local/lib/python3.12/site-packages/fastapi/applications.py:4495
../../local/lib/python3.12/site-packages/fastapi/applications.py:4495
  /usr/local/lib/python3.12/site-packages/fastapi/applications.py:4495: DeprecationWarning:
          on_event is deprecated, use lifespan event handlers instead.

          Read more about it in the
          [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).

    return self.router.on_event(event_type)

../../local/lib/python3.12/site-packages/tortoise/contrib/fastapi/__init__.py:100
../../local/lib/python3.12/site-packages/tortoise/contrib/fastapi/__init__.py:100
  /usr/local/lib/python3.12/site-packages/tortoise/contrib/fastapi/__init__.py:100: DeprecationWarning:
          on_event is deprecated, use lifespan event handlers instead.

          Read more about it in the
          [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).

    @app.on_event("shutdown")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================================= 1 passed, 8 warnings in 0.14s ========================================

Tried changing the code for:

@app.on_event("startup")
async def startup_event():
    # Setup Tortoise ORM (instead of register_tortoise)
    await Tortoise.init(config=TORTOISE_ORM)
    await Tortoise.generate_schemas()

but I'm still getting the same warning.

0

There are 0 answers