Generic typing for TestClient #1768
-
I'm using Starlette's import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from myapp import make_app
from myapp.config import get_settings, Settings
@pytest.fixture
def app() -> FastAPI:
return make_app()
@pytest.fixture
def client(app: FastAPI) -> TestClient:
return TestClient(app)
def test_dev_env(client: TestClient):
client.app.dependency_overrides[get_settings] = lambda: Settings(environment="dev")
response = client.get("/example")
# make assertions here When I run this code, it works just fine, but when I ask Mypy to typecheck my code, I get the following error:
After looking at the code that defines the def test_dev_env(client: TestClient[FastAPI]):
... Which would allow Mypy to typecheck my code and verify that I am using the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I just opened a pull request to demonstrate what I mean. |
Beta Was this translation helpful? Give feedback.
-
Could you also inject your app fixture? def test_dev_env(client: TestClient, app: FastAPI):
...
``` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
ASGIApp
should be a protocol, that would solve the issue here.