Skip to content

Commit 78fcd54

Browse files
TechNiickScirlat DanutKludex
authored
Create types module inside tests (#2502)
* Create types module inside tests * Apply suggestions from code review * Apply suggestions from code review * Fix check errors * Change testclientfactory due to autotest * No cover fix * Apply suggestions from code review * Skip code coverage for TestClientFactory protocol * Apply suggestions from code review * Small improvements * Lint * Fix everything --------- Co-authored-by: Scirlat Danut <[email protected]> Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 296cab9 commit 78fcd54

26 files changed

+66
-95
lines changed

tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from __future__ import annotations
22

33
import functools
4-
from typing import Any, Callable, Literal
4+
from typing import Any, Literal
55

66
import pytest
77

88
from starlette.testclient import TestClient
9-
10-
TestClientFactory = Callable[..., TestClient]
9+
from tests.types import TestClientFactory
1110

1211

1312
@pytest.fixture

tests/middleware/test_base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import (
66
Any,
77
AsyncGenerator,
8-
Callable,
98
Generator,
109
)
1110

@@ -23,8 +22,7 @@
2322
from starlette.testclient import TestClient
2423
from starlette.types import ASGIApp, Message, Receive, Scope, Send
2524
from starlette.websockets import WebSocket
26-
27-
TestClientFactory = Callable[[ASGIApp], TestClient]
25+
from tests.types import TestClientFactory
2826

2927

3028
class CustomMiddleware(BaseHTTPMiddleware):

tests/middleware/test_cors.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
from typing import Callable
2-
31
from starlette.applications import Starlette
42
from starlette.middleware import Middleware
53
from starlette.middleware.cors import CORSMiddleware
64
from starlette.requests import Request
75
from starlette.responses import PlainTextResponse
86
from starlette.routing import Route
9-
from starlette.testclient import TestClient
10-
from starlette.types import ASGIApp
11-
12-
TestClientFactory = Callable[[ASGIApp], TestClient]
7+
from tests.types import TestClientFactory
138

149

1510
def test_cors_allow_all(

tests/middleware/test_errors.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable
1+
from typing import Any
22

33
import pytest
44

@@ -8,10 +8,8 @@
88
from starlette.requests import Request
99
from starlette.responses import JSONResponse, Response
1010
from starlette.routing import Route
11-
from starlette.testclient import TestClient
1211
from starlette.types import Receive, Scope, Send
13-
14-
TestClientFactory = Callable[..., TestClient]
12+
from tests.types import TestClientFactory
1513

1614

1715
def test_handler(

tests/middleware/test_gzip.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
from typing import Callable
2-
31
from starlette.applications import Starlette
42
from starlette.middleware import Middleware
53
from starlette.middleware.gzip import GZipMiddleware
64
from starlette.requests import Request
75
from starlette.responses import ContentStream, PlainTextResponse, StreamingResponse
86
from starlette.routing import Route
9-
from starlette.testclient import TestClient
10-
from starlette.types import ASGIApp
11-
12-
TestClientFactory = Callable[[ASGIApp], TestClient]
7+
from tests.types import TestClientFactory
138

149

1510
def test_gzip_responses(test_client_factory: TestClientFactory) -> None:

tests/middleware/test_https_redirect.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
from typing import Callable
2-
31
from starlette.applications import Starlette
42
from starlette.middleware import Middleware
53
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
64
from starlette.requests import Request
75
from starlette.responses import PlainTextResponse
86
from starlette.routing import Route
9-
from starlette.testclient import TestClient
10-
11-
TestClientFactory = Callable[..., TestClient]
7+
from tests.types import TestClientFactory
128

139

1410
def test_https_redirect_middleware(test_client_factory: TestClientFactory) -> None:

tests/middleware/test_session.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import re
2-
from typing import Callable
32

43
from starlette.applications import Starlette
54
from starlette.middleware import Middleware
@@ -8,8 +7,7 @@
87
from starlette.responses import JSONResponse
98
from starlette.routing import Mount, Route
109
from starlette.testclient import TestClient
11-
12-
TestClientFactory = Callable[..., TestClient]
10+
from tests.types import TestClientFactory
1311

1412

1513
def view_session(request: Request) -> JSONResponse:

tests/middleware/test_trusted_host.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
from typing import Callable
2-
31
from starlette.applications import Starlette
42
from starlette.middleware import Middleware
53
from starlette.middleware.trustedhost import TrustedHostMiddleware
64
from starlette.requests import Request
75
from starlette.responses import PlainTextResponse
86
from starlette.routing import Route
9-
from starlette.testclient import TestClient
10-
11-
TestClientFactory = Callable[..., TestClient]
7+
from tests.types import TestClientFactory
128

139

1410
def test_trusted_host_middleware(test_client_factory: TestClientFactory) -> None:

tests/middleware/test_wsgi.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
from starlette._utils import collapse_excgroups
77
from starlette.middleware.wsgi import WSGIMiddleware, build_environ
8-
from starlette.testclient import TestClient
8+
from tests.types import TestClientFactory
99

1010
WSGIResponse = Iterable[bytes]
11-
TestClientFactory = Callable[..., TestClient]
1211
StartResponse = Callable[..., Any]
1312
Environment = Dict[str, Any]
1413

tests/test_applications.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from contextlib import asynccontextmanager
33
from pathlib import Path
4-
from typing import AsyncGenerator, AsyncIterator, Callable, Generator
4+
from typing import AsyncGenerator, AsyncIterator, Generator
55

66
import anyio
77
import pytest
@@ -20,8 +20,7 @@
2020
from starlette.testclient import TestClient
2121
from starlette.types import ASGIApp, Receive, Scope, Send
2222
from starlette.websockets import WebSocket
23-
24-
TestClientFactory = Callable[..., TestClient]
23+
from tests.types import TestClientFactory
2524

2625

2726
async def error_500(request: Request, exc: HTTPException) -> JSONResponse:

tests/test_authentication.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
from starlette.requests import HTTPConnection, Request
2222
from starlette.responses import JSONResponse, Response
2323
from starlette.routing import Route, WebSocketRoute
24-
from starlette.testclient import TestClient
2524
from starlette.websockets import WebSocket, WebSocketDisconnect
25+
from tests.types import TestClientFactory
2626

27-
TestClientFactory = Callable[..., TestClient]
2827
AsyncEndpoint = Callable[..., Awaitable[Response]]
2928
SyncEndpoint = Callable[..., Response]
3029

tests/test_background.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
from typing import Callable
2-
31
import pytest
42

53
from starlette.background import BackgroundTask, BackgroundTasks
64
from starlette.responses import Response
7-
from starlette.testclient import TestClient
85
from starlette.types import Receive, Scope, Send
9-
10-
TestClientFactory = Callable[..., TestClient]
6+
from tests.types import TestClientFactory
117

128

139
def test_async_task(test_client_factory: TestClientFactory) -> None:

tests/test_concurrency.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from contextvars import ContextVar
2-
from typing import Callable, Iterator
2+
from typing import Iterator
33

44
import anyio
55
import pytest
@@ -9,9 +9,7 @@
99
from starlette.requests import Request
1010
from starlette.responses import Response
1111
from starlette.routing import Route
12-
from starlette.testclient import TestClient
13-
14-
TestClientFactory = Callable[..., TestClient]
12+
from tests.types import TestClientFactory
1513

1614

1715
@pytest.mark.anyio

tests/test_convertors.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import Callable, Iterator
2+
from typing import Iterator
33

44
import pytest
55

@@ -8,9 +8,7 @@
88
from starlette.requests import Request
99
from starlette.responses import JSONResponse
1010
from starlette.routing import Route, Router
11-
from starlette.testclient import TestClient
12-
13-
TestClientFactory = Callable[..., TestClient]
11+
from tests.types import TestClientFactory
1412

1513

1614
@pytest.fixture(scope="module", autouse=True)

tests/test_endpoints.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable, Iterator
1+
from typing import Iterator
22

33
import pytest
44

@@ -8,8 +8,7 @@
88
from starlette.routing import Route, Router
99
from starlette.testclient import TestClient
1010
from starlette.websockets import WebSocket
11-
12-
TestClientFactory = Callable[..., TestClient]
11+
from tests.types import TestClientFactory
1312

1413

1514
class Homepage(HTTPEndpoint):

tests/test_exceptions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import warnings
2-
from typing import Callable, Generator
2+
from typing import Generator
33

44
import pytest
55

@@ -10,8 +10,7 @@
1010
from starlette.routing import Route, Router, WebSocketRoute
1111
from starlette.testclient import TestClient
1212
from starlette.types import Receive, Scope, Send
13-
14-
TestClientFactory = Callable[..., TestClient]
13+
from tests.types import TestClientFactory
1514

1615

1716
def raise_runtime_error(request: Request) -> None:

tests/test_formparsers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
from starlette.requests import Request
1414
from starlette.responses import JSONResponse
1515
from starlette.routing import Mount
16-
from starlette.testclient import TestClient
1716
from starlette.types import ASGIApp, Receive, Scope, Send
18-
19-
TestClientFactory = typing.Callable[..., TestClient]
17+
from tests.types import TestClientFactory
2018

2119

2220
class ForceMultipartDict(typing.Dict[typing.Any, typing.Any]):

tests/test_requests.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
from __future__ import annotations
22

33
import sys
4-
from typing import Any, Callable, Iterator
4+
from typing import Any, Iterator
55

66
import anyio
77
import pytest
88

99
from starlette.datastructures import Address, State
1010
from starlette.requests import ClientDisconnect, Request
1111
from starlette.responses import JSONResponse, PlainTextResponse, Response
12-
from starlette.testclient import TestClient
1312
from starlette.types import Message, Receive, Scope, Send
14-
15-
TestClientFactory = Callable[..., TestClient]
13+
from tests.types import TestClientFactory
1614

1715

1816
def test_request_url(test_client_factory: TestClientFactory) -> None:

tests/test_responses.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from http.cookies import SimpleCookie
77
from pathlib import Path
8-
from typing import AsyncIterator, Callable, Iterator
8+
from typing import AsyncIterator, Iterator
99

1010
import anyio
1111
import pytest
@@ -23,8 +23,7 @@
2323
)
2424
from starlette.testclient import TestClient
2525
from starlette.types import Message, Receive, Scope, Send
26-
27-
TestClientFactory = Callable[..., TestClient]
26+
from tests.types import TestClientFactory
2827

2928

3029
def test_text_response(test_client_factory: TestClientFactory) -> None:

tests/test_routing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from starlette.testclient import TestClient
1818
from starlette.types import ASGIApp, Message, Receive, Scope, Send
1919
from starlette.websockets import WebSocket, WebSocketDisconnect
20-
21-
TestClientFactory = typing.Callable[..., TestClient]
20+
from tests.types import TestClientFactory
2221

2322

2423
def homepage(request: Request) -> Response:

tests/test_schemas.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
from typing import Callable
2-
31
from starlette.applications import Starlette
42
from starlette.endpoints import HTTPEndpoint
53
from starlette.requests import Request
64
from starlette.responses import Response
75
from starlette.routing import Host, Mount, Route, Router, WebSocketRoute
86
from starlette.schemas import SchemaGenerator
9-
from starlette.testclient import TestClient
107
from starlette.websockets import WebSocket
8+
from tests.types import TestClientFactory
119

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

16-
TestClientFactory = Callable[..., TestClient]
17-
1814

1915
def ws(session: WebSocket) -> None:
2016
"""ws"""

tests/test_staticfiles.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
from starlette.responses import Response
1717
from starlette.routing import Mount
1818
from starlette.staticfiles import StaticFiles
19-
from starlette.testclient import TestClient
20-
21-
TestClientFactory = typing.Callable[..., TestClient]
19+
from tests.types import TestClientFactory
2220

2321

2422
def test_staticfiles(tmpdir: Path, test_client_factory: TestClientFactory) -> None:

tests/test_templates.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import typing
54
from pathlib import Path
65
from unittest import mock
76

@@ -16,9 +15,7 @@
1615
from starlette.responses import Response
1716
from starlette.routing import Route
1817
from starlette.templating import Jinja2Templates
19-
from starlette.testclient import TestClient
20-
21-
TestClientFactory = typing.Callable[..., TestClient]
18+
from tests.types import TestClientFactory
2219

2320

2421
def test_templates(tmpdir: Path, test_client_factory: TestClientFactory) -> None:

0 commit comments

Comments
 (0)