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_schemas.py #2490

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
from typing import Callable

from starlette.applications import Starlette
from starlette.endpoints import HTTPEndpoint
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Host, Mount, Route, Router, WebSocketRoute
from starlette.schemas import SchemaGenerator
from starlette.testclient import TestClient
from starlette.websockets import WebSocket

schemas = SchemaGenerator(
{"openapi": "3.0.0", "info": {"title": "Example API", "version": "1.0"}}
)

TestClientFactory = Callable[..., TestClient]


def ws(session):
def ws(session: WebSocket) -> None:
"""ws"""
pass # pragma: no cover


def get_user(request):
def get_user(request: Request) -> None:
"""
responses:
200:
Expand All @@ -24,7 +32,7 @@ def get_user(request):
pass # pragma: no cover


def list_users(request):
def list_users(request: Request) -> None:
"""
responses:
200:
Expand All @@ -35,7 +43,7 @@ def list_users(request):
pass # pragma: no cover


def create_user(request):
def create_user(request: Request) -> None:
"""
responses:
200:
Expand All @@ -47,7 +55,7 @@ def create_user(request):


class OrganisationsEndpoint(HTTPEndpoint):
def get(self, request):
def get(self, request: Request) -> None:
"""
responses:
200:
Expand All @@ -57,7 +65,7 @@ def get(self, request):
"""
pass # pragma: no cover

def post(self, request):
def post(self, request: Request) -> None:
"""
responses:
200:
Expand All @@ -68,7 +76,7 @@ def post(self, request):
pass # pragma: no cover


def regular_docstring_and_schema(request):
def regular_docstring_and_schema(request: Request) -> None:
"""
This a regular docstring example (not included in schema)

Expand All @@ -81,18 +89,18 @@ def regular_docstring_and_schema(request):
pass # pragma: no cover


def regular_docstring(request):
def regular_docstring(request: Request) -> None:
"""
This a regular docstring example (not included in schema)
"""
pass # pragma: no cover


def no_docstring(request):
def no_docstring(request: Request) -> None:
pass # pragma: no cover


def subapp_endpoint(request):
def subapp_endpoint(request: Request) -> None:
"""
responses:
200:
Expand All @@ -101,7 +109,7 @@ def subapp_endpoint(request):
pass # pragma: no cover


def schema(request):
def schema(request: Request) -> Response:
return schemas.OpenAPIResponse(request=request)


Expand All @@ -128,7 +136,7 @@ def schema(request):
)


def test_schema_generation():
def test_schema_generation() -> None:
schema = schemas.get_schema(routes=app.routes)
assert schema == {
"openapi": "3.0.0",
Expand Down Expand Up @@ -261,7 +269,7 @@ def test_schema_generation():
"""


def test_schema_endpoint(test_client_factory):
def test_schema_endpoint(test_client_factory: TestClientFactory) -> None:
client = test_client_factory(app)
response = client.get("/schema")
assert response.headers["Content-Type"] == "application/vnd.oai.openapi"
Expand Down