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

Create types module inside tests #2502

Merged
merged 12 commits into from
Jul 27, 2024
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import annotations

import functools
from typing import Any, Callable, Literal
from typing import Any, Literal

import pytest

from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


@pytest.fixture
Expand Down
4 changes: 1 addition & 3 deletions tests/middleware/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import (
Any,
AsyncGenerator,
Callable,
Generator,
)

Expand All @@ -23,8 +22,7 @@
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from starlette.websockets import WebSocket

TestClientFactory = Callable[[ASGIApp], TestClient]
from tests.types import TestClientFactory


class CustomMiddleware(BaseHTTPMiddleware):
Expand Down
7 changes: 1 addition & 6 deletions tests/middleware/test_cors.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
from typing import Callable

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from starlette.testclient import TestClient
from starlette.types import ASGIApp

TestClientFactory = Callable[[ASGIApp], TestClient]
from tests.types import TestClientFactory


def test_cors_allow_all(
Expand Down
6 changes: 2 additions & 4 deletions tests/middleware/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable
from typing import Any

import pytest

Expand All @@ -8,10 +8,8 @@
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.routing import Route
from starlette.testclient import TestClient
from starlette.types import Receive, Scope, Send

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def test_handler(
Expand Down
7 changes: 1 addition & 6 deletions tests/middleware/test_gzip.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
from typing import Callable

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.gzip import GZipMiddleware
from starlette.requests import Request
from starlette.responses import ContentStream, PlainTextResponse, StreamingResponse
from starlette.routing import Route
from starlette.testclient import TestClient
from starlette.types import ASGIApp

TestClientFactory = Callable[[ASGIApp], TestClient]
from tests.types import TestClientFactory


def test_gzip_responses(test_client_factory: TestClientFactory) -> None:
Expand Down
6 changes: 1 addition & 5 deletions tests/middleware/test_https_redirect.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from typing import Callable

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def test_https_redirect_middleware(test_client_factory: TestClientFactory) -> None:
Expand Down
4 changes: 1 addition & 3 deletions tests/middleware/test_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import re
from typing import Callable

from starlette.applications import Starlette
from starlette.middleware import Middleware
Expand All @@ -8,8 +7,7 @@
from starlette.responses import JSONResponse
from starlette.routing import Mount, Route
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def view_session(request: Request) -> JSONResponse:
Expand Down
6 changes: 1 addition & 5 deletions tests/middleware/test_trusted_host.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from typing import Callable

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def test_trusted_host_middleware(test_client_factory: TestClientFactory) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/middleware/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

from starlette._utils import collapse_excgroups
from starlette.middleware.wsgi import WSGIMiddleware, build_environ
from starlette.testclient import TestClient
from tests.types import TestClientFactory

WSGIResponse = Iterable[bytes]
TestClientFactory = Callable[..., TestClient]
StartResponse = Callable[..., Any]
Environment = Dict[str, Any]

Expand Down
5 changes: 2 additions & 3 deletions tests/test_applications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from contextlib import asynccontextmanager
from pathlib import Path
from typing import AsyncGenerator, AsyncIterator, Callable, Generator
from typing import AsyncGenerator, AsyncIterator, Generator

import anyio
import pytest
Expand All @@ -20,8 +20,7 @@
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Receive, Scope, Send
from starlette.websockets import WebSocket

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


async def error_500(request: Request, exc: HTTPException) -> JSONResponse:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
from starlette.requests import HTTPConnection, Request
from starlette.responses import JSONResponse, Response
from starlette.routing import Route, WebSocketRoute
from starlette.testclient import TestClient
from starlette.websockets import WebSocket, WebSocketDisconnect
from tests.types import TestClientFactory

TestClientFactory = Callable[..., TestClient]
AsyncEndpoint = Callable[..., Awaitable[Response]]
SyncEndpoint = Callable[..., Response]

Expand Down
6 changes: 1 addition & 5 deletions tests/test_background.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from typing import Callable

import pytest

from starlette.background import BackgroundTask, BackgroundTasks
from starlette.responses import Response
from starlette.testclient import TestClient
from starlette.types import Receive, Scope, Send

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def test_async_task(test_client_factory: TestClientFactory) -> None:
Expand Down
6 changes: 2 additions & 4 deletions tests/test_concurrency.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextvars import ContextVar
from typing import Callable, Iterator
from typing import Iterator

import anyio
import pytest
Expand All @@ -9,9 +9,7 @@
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


@pytest.mark.anyio
Expand Down
6 changes: 2 additions & 4 deletions tests/test_convertors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Callable, Iterator
from typing import Iterator

import pytest

Expand All @@ -8,9 +8,7 @@
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route, Router
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


@pytest.fixture(scope="module", autouse=True)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Iterator
from typing import Iterator

import pytest

Expand All @@ -8,8 +8,7 @@
from starlette.routing import Route, Router
from starlette.testclient import TestClient
from starlette.websockets import WebSocket

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


class Homepage(HTTPEndpoint):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from typing import Callable, Generator
from typing import Generator

import pytest

Expand All @@ -10,8 +10,7 @@
from starlette.routing import Route, Router, WebSocketRoute
from starlette.testclient import TestClient
from starlette.types import Receive, Scope, Send

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def raise_runtime_error(request: Request) -> None:
Expand Down
4 changes: 1 addition & 3 deletions tests/test_formparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Mount
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Receive, Scope, Send

TestClientFactory = typing.Callable[..., TestClient]
from tests.types import TestClientFactory


class ForceMultipartDict(typing.Dict[typing.Any, typing.Any]):
Expand Down
6 changes: 2 additions & 4 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from __future__ import annotations

import sys
from typing import Any, Callable, Iterator
from typing import Any, Iterator

import anyio
import pytest

from starlette.datastructures import Address, State
from starlette.requests import ClientDisconnect, Request
from starlette.responses import JSONResponse, PlainTextResponse, Response
from starlette.testclient import TestClient
from starlette.types import Message, Receive, Scope, Send

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def test_request_url(test_client_factory: TestClientFactory) -> None:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
from http.cookies import SimpleCookie
from pathlib import Path
from typing import AsyncIterator, Callable, Iterator
from typing import AsyncIterator, Iterator

import anyio
import pytest
Expand All @@ -23,8 +23,7 @@
)
from starlette.testclient import TestClient
from starlette.types import Message, Receive, Scope, Send

TestClientFactory = Callable[..., TestClient]
from tests.types import TestClientFactory


def test_text_response(test_client_factory: TestClientFactory) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from starlette.websockets import WebSocket, WebSocketDisconnect

TestClientFactory = typing.Callable[..., TestClient]
from tests.types import TestClientFactory


def homepage(request: Request) -> Response:
Expand Down
6 changes: 1 addition & 5 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
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
from tests.types import TestClientFactory

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

TestClientFactory = Callable[..., TestClient]


def ws(session: WebSocket) -> None:
"""ws"""
Expand Down
4 changes: 1 addition & 3 deletions tests/test_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
from starlette.responses import Response
from starlette.routing import Mount
from starlette.staticfiles import StaticFiles
from starlette.testclient import TestClient

TestClientFactory = typing.Callable[..., TestClient]
from tests.types import TestClientFactory


def test_staticfiles(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
Expand Down
5 changes: 1 addition & 4 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import os
import typing
from pathlib import Path
from unittest import mock

Expand All @@ -16,9 +15,7 @@
from starlette.responses import Response
from starlette.routing import Route
from starlette.templating import Jinja2Templates
from starlette.testclient import TestClient

TestClientFactory = typing.Callable[..., TestClient]
from tests.types import TestClientFactory


def test_templates(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
Expand Down
Loading