Skip to content

Commit 14919aa

Browse files
committed
Provide typed pytest fixture helper
1 parent 14590ab commit 14919aa

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

app/tests/conftest.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import os
22
import time
3-
from typing import Optional
3+
from typing import Any, Callable, Optional, TypeVar, cast
44
from urllib.parse import quote_plus
55

66
import pytest
77

88
os.environ.setdefault("OTEL_SDK_DISABLED", "true")
99

10+
F = TypeVar("F", bound=Callable[..., object])
11+
12+
13+
def typed_fixture(*args: Any, **kwargs: Any) -> Callable[[F], F]:
14+
"""Typed veneer over pytest.fixture to keep mypy happy while preserving runtime behavior."""
15+
fixture_factory = pytest.fixture(*args, **kwargs)
16+
17+
def decorator(func: F) -> F:
18+
return cast(F, fixture_factory(func))
19+
20+
return decorator
21+
1022
TRANSIENT_CODES = {
1123
"08000",
1224
"08001",
@@ -102,6 +114,6 @@ def wait_for_database(dsn: str, timeout: float = 30.0) -> None:
102114
time.sleep(min(0.25 * attempt, 2.0))
103115

104116

105-
@pytest.fixture(scope="session", autouse=True)
117+
@typed_fixture(scope="session", autouse=True)
106118
def _ensure_database_ready() -> None:
107119
wait_for_database(resolve_dsn())

app/tests/test_main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import os
22
from typing import Any, Generator
33

4-
import pytest
5-
64
os.environ.setdefault("OTEL_SDK_DISABLED", "true")
75

86
from app.main import app # noqa: E402
7+
from app.tests.conftest import typed_fixture
98

109
TestClient = Any
1110

1211

13-
@pytest.fixture
12+
@typed_fixture
1413
def client() -> Generator[Any, None, None]:
1514
"""Create a test client for the Flask app."""
1615
app.config["TESTING"] = True

0 commit comments

Comments
 (0)