1- import asyncio
2- import warnings
3- from typing import Any , Awaitable , Callable , Dict , Generator , Optional , Type , Union
1+ from typing import (
2+ Any ,
3+ Awaitable ,
4+ Dict ,
5+ Iterator ,
6+ Optional ,
7+ Protocol ,
8+ Type ,
9+ TypeVar ,
10+ Union ,
11+ overload ,
12+ )
413
514import pytest
615import pytest_asyncio
716from aiohttp .test_utils import BaseTestServer , RawTestServer , TestClient , TestServer
8- from aiohttp .web import Application , BaseRequest , StreamResponse
17+ from aiohttp .web import Application , BaseRequest , Request
18+ from aiohttp .web_protocol import _RequestHandler
19+
20+ _Request = TypeVar ("_Request" , bound = BaseRequest )
21+
922
10- AiohttpClient = Callable [[Union [Application , BaseTestServer ]], Awaitable [TestClient ]]
23+ class AiohttpClient (Protocol ):
24+ @overload
25+ async def __call__ (
26+ self ,
27+ __param : Application ,
28+ * ,
29+ server_kwargs : Optional [Dict [str , Any ]] = None ,
30+ ** kwargs : Any ,
31+ ) -> TestClient [Request , Application ]: ...
32+
33+ @overload
34+ async def __call__ (
35+ self ,
36+ __param : BaseTestServer , # TODO(aiohttp4): BaseTestServer[_Request]
37+ * ,
38+ server_kwargs : Optional [Dict [str , Any ]] = None ,
39+ ** kwargs : Any ,
40+ ) -> TestClient [_Request , None ]: ...
41+
42+
43+ class AiohttpServer (Protocol ):
44+ def __call__ (
45+ self , app : Application , * , port : Optional [int ] = None , ** kwargs : Any
46+ ) -> Awaitable [TestServer ]: ...
47+
48+
49+ class AiohttpRawServer (Protocol ):
50+ def __call__ (
51+ self ,
52+ handler : _RequestHandler , # TODO(aiohttp4): _RequestHandler[BaseRequest]
53+ * ,
54+ port : Optional [int ] = None ,
55+ ** kwargs : Any ,
56+ ) -> Awaitable [RawTestServer ]: ...
1157
1258
1359LEGACY_MODE = DeprecationWarning (
@@ -28,41 +74,8 @@ def pytest_configure(config) -> None:
2874 config .issue_config_time_warning (LEGACY_MODE , stacklevel = 2 )
2975
3076
31- @pytest .fixture
32- def loop (event_loop : asyncio .AbstractEventLoop ) -> asyncio .AbstractEventLoop :
33- warnings .warn (
34- "'loop' fixture is deprecated and scheduled for removal, "
35- "please use 'event_loop' instead" ,
36- DeprecationWarning ,
37- )
38- return event_loop
39-
40-
41- @pytest .fixture
42- def proactor_loop (event_loop : asyncio .AbstractEventLoop ) -> asyncio .AbstractEventLoop :
43- warnings .warn (
44- "'proactor_loop' fixture is deprecated and scheduled for removal, "
45- "please use 'event_loop' instead" ,
46- DeprecationWarning ,
47- )
48- return event_loop
49-
50-
51- @pytest .fixture
52- def aiohttp_unused_port (
53- unused_tcp_port_factory : Callable [[], int ]
54- ) -> Callable [[], int ]:
55- warnings .warn (
56- "'aiohttp_unused_port' fixture is deprecated "
57- "and scheduled for removal, "
58- "please use 'unused_tcp_port_factory' instead" ,
59- DeprecationWarning ,
60- )
61- return unused_tcp_port_factory
62-
63-
6477@pytest_asyncio .fixture
65- async def aiohttp_server () -> Callable [..., Awaitable [ TestServer ] ]:
78+ async def aiohttp_server () -> Iterator [ AiohttpServer ]:
6679 """Factory to create a TestServer instance, given an app.
6780
6881 aiohttp_server(app, **kwargs)
@@ -84,15 +97,15 @@ async def go(
8497
8598
8699@pytest_asyncio .fixture
87- async def aiohttp_raw_server () -> Callable [..., Awaitable [ RawTestServer ] ]:
100+ async def aiohttp_raw_server () -> Iterator [ AiohttpRawServer ]:
88101 """Factory to create a RawTestServer instance, given a web handler.
89102
90103 aiohttp_raw_server(handler, **kwargs)
91104 """
92105 servers = []
93106
94107 async def go (
95- handler : Callable [[ BaseRequest ], Awaitable [ StreamResponse ]],
108+ handler : _RequestHandler , # TODO(aiohttp4): _RequestHandler[BaseRequest]
96109 * ,
97110 port : Optional [int ] = None ,
98111 ** kwargs : Any ,
@@ -108,8 +121,8 @@ async def go(
108121 await servers .pop ().close ()
109122
110123
111- @pytest .fixture
112- def aiohttp_client_cls () -> Type [TestClient ]:
124+ @pytest_asyncio .fixture
125+ def aiohttp_client_cls () -> Type [TestClient [ Any , Any ] ]:
113126 """
114127 Client class to use in ``aiohttp_client`` factory.
115128
@@ -137,8 +150,8 @@ def test_login(aiohttp_client):
137150
138151@pytest_asyncio .fixture
139152async def aiohttp_client (
140- aiohttp_client_cls : Type [TestClient ],
141- ) -> Generator [AiohttpClient , None , None ]:
153+ aiohttp_client_cls : Type [TestClient [ Any , Any ]]
154+ ) -> Iterator [AiohttpClient ]:
142155 """Factory to create a TestClient instance.
143156
144157 aiohttp_client(app, **kwargs)
@@ -147,12 +160,30 @@ async def aiohttp_client(
147160 """
148161 clients = []
149162
163+ @overload
164+ async def go (
165+ __param : Application ,
166+ * ,
167+ server_kwargs : Optional [Dict [str , Any ]] = None ,
168+ ** kwargs : Any ,
169+ ) -> TestClient [Request , Application ]: ...
170+
171+ @overload
172+ async def go (
173+ __param : BaseTestServer , # TODO(aiohttp4): BaseTestServer[_Request]
174+ * ,
175+ server_kwargs : Optional [Dict [str , Any ]] = None ,
176+ ** kwargs : Any ,
177+ ) -> TestClient [_Request , None ]: ...
178+
150179 async def go (
151- __param : Union [Application , BaseTestServer ],
180+ __param : Union [
181+ Application , BaseTestServer
182+ ], # TODO(aiohttp4): BaseTestServer[Any]
152183 * ,
153184 server_kwargs : Optional [Dict [str , Any ]] = None ,
154185 ** kwargs : Any ,
155- ) -> TestClient :
186+ ) -> TestClient [ Any , Any ] :
156187 if isinstance (__param , Application ):
157188 server_kwargs = server_kwargs or {}
158189 server = TestServer (__param , ** server_kwargs )
0 commit comments