Skip to content

Deprecation Warning for **env_options in Jinja2Templates with Starlette >=0.28.0 #574

@ptrstn

Description

@ptrstn

I'm encountering a deprecation warning related to Jinja2Templates when running pytest tests with starlette>=0.28.0 :

.venv/lib/python3.11/site-packages/starlette/templating.py:106: DeprecationWarning: Extra environment options are deprecated. Use a preconfigured jinja2.Environment instead.
    warnings.warn(

This warning does not appear when installing starlette==0.27.0

According to the Starlette 0.28.0 release notes, the **env_options parameter in Jinja2Templates has been deprecated in favor of the new env parameter. The relevant pull request #2159 explains this change.

It seems likely that Starlette Admin (or other dependencies) are still using the deprecated **env_options, causing the warning when running tests with Starlette 0.28.0.

Steps to Reproduce:

  1. Project Structure:

    .
    ├── app.py
    ├── requirements-deprecation-warning.txt
    ├── requirements-no-warnings.txt
    └── test_app.py
    
  2. Source Code:

    app.py:

    from fastapi import FastAPI
    from starlette_admin.contrib.sqlmodel import Admin
    from sqlmodel import SQLModel, create_engine
    
    DATABASE_URL = "sqlite:///./test.db"
    engine = create_engine(DATABASE_URL, echo=True)
    SQLModel.metadata.create_all(engine)
    
    app = FastAPI()
    admin = Admin(engine, title="Simple Admin")
    admin.mount_to(app)

    test_app.py:

    import pytest
    from fastapi.testclient import TestClient
    from app import app
    
    @pytest.fixture(scope="module")
    def client():
        with TestClient(app) as client:
            yield client
    
    def test_admin_page(client):
        response = client.get("/admin")
        assert response.status_code == 200
        assert "Simple Admin" in response.text
  3. Requirements Files:

    requirements-deprecation-warning.txt:

    fastapi
    sqlmodel
    uvicorn
    starlette-admin
    jinja2
    sqlalchemy
    starlette==0.28.0
    pytest
    httpx==0.24.1
    

    requirements-no-warnings.txt:

    fastapi
    sqlmodel
    uvicorn
    starlette-admin
    jinja2
    sqlalchemy
    starlette==0.27.0
    pytest
    httpx==0.24.1
    
  4. Reproduce the Warning:

    pip install -r requirements-deprecation-warning.txt
    pytest

    Result: Deprecation Warning

    pip install -r requirements-no-warnings.txt
    pytest

    Result: All tests pass without warnings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions