Skip to content
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

Add type hints to test_convertors.py #2476

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions tests/test_convertors.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from datetime import datetime
from typing import Any, Callable, Generator
Kludex marked this conversation as resolved.
Show resolved Hide resolved

import pytest

from starlette import convertors
from starlette.convertors import Convertor, register_url_convertor
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route, Router
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]


@pytest.fixture(scope="module", autouse=True)
def refresh_convertor_types():
def refresh_convertor_types() -> Generator[Any, None, None]:
Kludex marked this conversation as resolved.
Show resolved Hide resolved
convert_types = convertors.CONVERTOR_TYPES.copy()
yield
convertors.CONVERTOR_TYPES = convert_types
Expand All @@ -29,7 +34,7 @@ def to_string(self, value: datetime) -> str:
def app() -> Router:
register_url_convertor("datetime", DateTimeConvertor())

def datetime_convertor(request):
def datetime_convertor(request: Request) -> JSONResponse:
param = request.path_params["param"]
assert isinstance(param, datetime)
return JSONResponse({"datetime": param.strftime("%Y-%m-%dT%H:%M:%S")})
Expand All @@ -45,7 +50,10 @@ def datetime_convertor(request):
)


def test_datetime_convertor(test_client_factory, app: Router):
def test_datetime_convertor(
test_client_factory: TestClientFactory,
app: Router,
) -> None:
Kludex marked this conversation as resolved.
Show resolved Hide resolved
client = test_client_factory(app)
response = client.get("/datetime/2020-01-01T00:00:00")
assert response.json() == {"datetime": "2020-01-01T00:00:00"}
Expand All @@ -57,8 +65,12 @@ def test_datetime_convertor(test_client_factory, app: Router):


@pytest.mark.parametrize("param, status_code", [("1.0", 200), ("1-0", 404)])
def test_default_float_convertor(test_client_factory, param: str, status_code: int):
def float_convertor(request):
def test_default_float_convertor(
test_client_factory: TestClientFactory,
param: str,
status_code: int,
Kludex marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
def float_convertor(request: Request) -> JSONResponse:
param = request.path_params["param"]
assert isinstance(param, float)
return JSONResponse({"float": param})
Expand Down