1
1
import asyncio
2
- import warnings
3
- from typing import Any , Awaitable , Callable , Dict , Generator , Optional , Type , Union
2
+ from typing import (
3
+ Any ,
4
+ Awaitable ,
5
+ Callable ,
6
+ Dict ,
7
+ Iterator ,
8
+ Optional ,
9
+ Protocol ,
10
+ Type ,
11
+ TypeVar ,
12
+ Union ,
13
+ overload ,
14
+ )
4
15
5
16
import pytest
6
17
import pytest_asyncio
7
18
from aiohttp .test_utils import BaseTestServer , RawTestServer , TestClient , TestServer
8
- from aiohttp .web import Application , BaseRequest , StreamResponse
19
+ from aiohttp .web import Application , BaseRequest , Request
20
+ from aiohttp .web_protocol import _RequestHandler
21
+
22
+ _Request = TypeVar ("_Request" , bound = BaseRequest )
9
23
10
- AiohttpClient = Callable [[Union [Application , BaseTestServer ]], Awaitable [TestClient ]]
24
+
25
+ class AiohttpClient (Protocol ):
26
+ @overload
27
+ async def __call__ (
28
+ self ,
29
+ __param : Application ,
30
+ * ,
31
+ server_kwargs : Optional [Dict [str , Any ]] = None ,
32
+ ** kwargs : Any
33
+ ) -> TestClient [Request , Application ]: ...
34
+ @overload
35
+ async def __call__ (
36
+ self ,
37
+ __param : BaseTestServer [_Request ],
38
+ * ,
39
+ server_kwargs : Optional [Dict [str , Any ]] = None ,
40
+ ** kwargs : Any
41
+ ) -> TestClient [_Request , None ]: ...
42
+
43
+
44
+ class AiohttpServer (Protocol ):
45
+ def __call__ (
46
+ self , app : Application , * , port : Optional [int ] = None , ** kwargs : Any
47
+ ) -> Awaitable [TestServer ]: ...
48
+
49
+
50
+ class AiohttpRawServer (Protocol ):
51
+ def __call__ (
52
+ self ,
53
+ handler : _RequestHandler [BaseRequest ],
54
+ * ,
55
+ port : Optional [int ] = None ,
56
+ ** kwargs : Any
57
+ ) -> Awaitable [RawTestServer ]: ...
11
58
12
59
13
60
LEGACY_MODE = DeprecationWarning (
@@ -28,41 +75,8 @@ def pytest_configure(config) -> None:
28
75
config .issue_config_time_warning (LEGACY_MODE , stacklevel = 2 )
29
76
30
77
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
78
@pytest_asyncio .fixture
65
- async def aiohttp_server () -> Callable [..., Awaitable [ TestServer ] ]:
79
+ async def aiohttp_server () -> Iterator [ AiohttpServer ]:
66
80
"""Factory to create a TestServer instance, given an app.
67
81
68
82
aiohttp_server(app, **kwargs)
@@ -84,18 +98,18 @@ async def go(
84
98
85
99
86
100
@pytest_asyncio .fixture
87
- async def aiohttp_raw_server () -> Callable [..., Awaitable [ RawTestServer ] ]:
101
+ async def aiohttp_raw_server () -> Iterator [ AiohttpRawServer ]:
88
102
"""Factory to create a RawTestServer instance, given a web handler.
89
103
90
104
aiohttp_raw_server(handler, **kwargs)
91
105
"""
92
106
servers = []
93
107
94
108
async def go (
95
- handler : Callable [[ BaseRequest ], Awaitable [ StreamResponse ] ],
109
+ handler : _RequestHandler [ BaseRequest ],
96
110
* ,
97
111
port : Optional [int ] = None ,
98
- ** kwargs : Any ,
112
+ ** kwargs : Any
99
113
) -> RawTestServer :
100
114
server = RawTestServer (handler , port = port )
101
115
await server .start_server (** kwargs )
@@ -108,8 +122,8 @@ async def go(
108
122
await servers .pop ().close ()
109
123
110
124
111
- @pytest .fixture
112
- def aiohttp_client_cls () -> Type [TestClient ]:
125
+ @pytest_asyncio .fixture
126
+ def aiohttp_client_cls () -> Type [TestClient [ Any , Any ] ]:
113
127
"""
114
128
Client class to use in ``aiohttp_client`` factory.
115
129
@@ -137,8 +151,8 @@ def test_login(aiohttp_client):
137
151
138
152
@pytest_asyncio .fixture
139
153
async def aiohttp_client (
140
- aiohttp_client_cls : Type [TestClient ],
141
- ) -> Generator [AiohttpClient , None , None ]:
154
+ aiohttp_client_cls : Type [TestClient [ Any , Any ]]
155
+ ) -> Iterator [AiohttpClient ]:
142
156
"""Factory to create a TestClient instance.
143
157
144
158
aiohttp_client(app, **kwargs)
@@ -147,12 +161,26 @@ async def aiohttp_client(
147
161
"""
148
162
clients = []
149
163
164
+ @overload
165
+ async def go (
166
+ __param : Application ,
167
+ * ,
168
+ server_kwargs : Optional [Dict [str , Any ]] = None ,
169
+ ** kwargs : Any
170
+ ) -> TestClient [Request , Application ]: ...
171
+ @overload
172
+ async def go (
173
+ __param : BaseTestServer [_Request ],
174
+ * ,
175
+ server_kwargs : Optional [Dict [str , Any ]] = None ,
176
+ ** kwargs : Any
177
+ ) -> TestClient [_Request , None ]: ...
150
178
async def go (
151
- __param : Union [Application , BaseTestServer ],
179
+ __param : Union [Application , BaseTestServer [ Any ] ],
152
180
* ,
153
181
server_kwargs : Optional [Dict [str , Any ]] = None ,
154
- ** kwargs : Any ,
155
- ) -> TestClient :
182
+ ** kwargs : Any
183
+ ) -> TestClient [ Any , Any ] :
156
184
if isinstance (__param , Application ):
157
185
server_kwargs = server_kwargs or {}
158
186
server = TestServer (__param , ** server_kwargs )
0 commit comments