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
+ )
4
13
5
14
import pytest
6
15
import pytest_asyncio
7
16
from 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
+
9
22
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 ]: ...
11
57
12
58
13
59
LEGACY_MODE = DeprecationWarning (
@@ -28,41 +74,8 @@ def pytest_configure(config) -> None:
28
74
config .issue_config_time_warning (LEGACY_MODE , stacklevel = 2 )
29
75
30
76
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
-
64
77
@pytest_asyncio .fixture
65
- async def aiohttp_server () -> Callable [..., Awaitable [ TestServer ] ]:
78
+ async def aiohttp_server () -> Iterator [ AiohttpServer ]:
66
79
"""Factory to create a TestServer instance, given an app.
67
80
68
81
aiohttp_server(app, **kwargs)
@@ -84,15 +97,15 @@ async def go(
84
97
85
98
86
99
@pytest_asyncio .fixture
87
- async def aiohttp_raw_server () -> Callable [..., Awaitable [ RawTestServer ] ]:
100
+ async def aiohttp_raw_server () -> Iterator [ AiohttpRawServer ]:
88
101
"""Factory to create a RawTestServer instance, given a web handler.
89
102
90
103
aiohttp_raw_server(handler, **kwargs)
91
104
"""
92
105
servers = []
93
106
94
107
async def go (
95
- handler : Callable [[ BaseRequest ], Awaitable [ StreamResponse ]],
108
+ handler : _RequestHandler , # TODO(aiohttp4): _RequestHandler[BaseRequest]
96
109
* ,
97
110
port : Optional [int ] = None ,
98
111
** kwargs : Any ,
@@ -108,8 +121,8 @@ async def go(
108
121
await servers .pop ().close ()
109
122
110
123
111
- @pytest .fixture
112
- def aiohttp_client_cls () -> Type [TestClient ]:
124
+ @pytest_asyncio .fixture
125
+ def aiohttp_client_cls () -> Type [TestClient [ Any , Any ] ]:
113
126
"""
114
127
Client class to use in ``aiohttp_client`` factory.
115
128
@@ -137,8 +150,8 @@ def test_login(aiohttp_client):
137
150
138
151
@pytest_asyncio .fixture
139
152
async 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 ]:
142
155
"""Factory to create a TestClient instance.
143
156
144
157
aiohttp_client(app, **kwargs)
@@ -147,12 +160,30 @@ async def aiohttp_client(
147
160
"""
148
161
clients = []
149
162
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
+
150
179
async def go (
151
- __param : Union [Application , BaseTestServer ],
180
+ __param : Union [
181
+ Application , BaseTestServer
182
+ ], # TODO(aiohttp4): BaseTestServer[Any]
152
183
* ,
153
184
server_kwargs : Optional [Dict [str , Any ]] = None ,
154
185
** kwargs : Any ,
155
- ) -> TestClient :
186
+ ) -> TestClient [ Any , Any ] :
156
187
if isinstance (__param , Application ):
157
188
server_kwargs = server_kwargs or {}
158
189
server = TestServer (__param , ** server_kwargs )
0 commit comments