File: pytest-asyncio-1.0.0.patch

package info (click to toggle)
aiosmtplib 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 644 kB
  • sloc: python: 5,515; makefile: 20; sh: 6
file content (276 lines) | stat: -rw-r--r-- 9,350 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
From: Colin Watson <cjwatson@debian.org>
Date: Tue, 16 Sep 2025 14:00:38 +0100
Subject: chore: port to pytest-asyncio 1.0.0

This mostly follows
https://pytest-asyncio.readthedocs.io/en/stable/how-to-guides/migrate_from_0_21.html;
I had to weaken one test a bit to avoid running into problems with
pytest-asyncio's finalizer code.

Origin: upstream, https://github.com/cole/aiosmtplib/pull/328
Last-Update: 2025-09-17
---
 pyproject.toml         |  2 +-
 requirements-dev.txt   |  2 +-
 tests/conftest.py      | 61 +++++++++++++++++++++++---------------------------
 tests/test_asyncio.py  |  8 ++++---
 tests/test_protocol.py | 18 +++++++--------
 5 files changed, 44 insertions(+), 47 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index b346864..3a5af57 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -46,7 +46,7 @@ path = "src/aiosmtplib/__init__.py"
 [tool.pytest.ini_options]
 pythonpath = "src"
 asyncio_mode = "auto"
-asyncio_default_fixture_loop_scope = "session"
+asyncio_default_fixture_loop_scope = "function"
 minversion = "6.0"
 junit_family = "xunit2"
 addopts = ["--import-mode=importlib", "--strict-markers"]
diff --git a/requirements-dev.txt b/requirements-dev.txt
index c463e82..3a4b4d9 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,5 +1,5 @@
 pytest>=7.2
-pytest-asyncio>=0.20.1,<1.0.0
+pytest-asyncio>=0.20.1
 pytest-cov>=4.0
 pytest-xdist>=3.0
 coverage[toml]>=6.5
diff --git a/tests/conftest.py b/tests/conftest.py
index fbb48ea..2637c20 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -10,7 +10,7 @@ import email.mime.text
 import socket
 import ssl
 import sys
-from collections.abc import Callable, Generator
+from collections.abc import AsyncGenerator, Callable, Generator
 from pathlib import Path
 from typing import Any, Optional, Union
 
@@ -101,9 +101,8 @@ def pytest_collection_modifyitems(
 
 
 @pytest_asyncio.fixture
-def debug_event_loop(
-    event_loop: asyncio.AbstractEventLoop,
-) -> Generator[asyncio.AbstractEventLoop, None, None]:
+async def debug_event_loop() -> AsyncGenerator[asyncio.AbstractEventLoop]:
+    event_loop = asyncio.get_running_loop()
     previous_debug = event_loop.get_debug()
     event_loop.set_debug(True)
 
@@ -461,14 +460,13 @@ def smtpd_factory(
     return factory
 
 
-@pytest.fixture(scope="function")
-def smtpd_server(
+@pytest_asyncio.fixture(scope="function")
+async def smtpd_server(
     request: pytest.FixtureRequest,
-    event_loop: asyncio.AbstractEventLoop,
     bind_address: str,
     server_tls_context: ssl.SSLContext,
     smtpd_factory: Callable[[], SMTPD],
-) -> Generator[asyncio.AbstractServer, None, None]:
+) -> AsyncGenerator[asyncio.AbstractServer]:
     smtpd_options_marker = request.node.get_closest_marker("smtpd_options")
     if smtpd_options_marker is None:
         smtpd_options = {}
@@ -483,62 +481,59 @@ def smtpd_server(
     if smtpd_options.get("tls", False):
         create_server_kwargs["ssl"] = server_tls_context
 
-    server_coro = event_loop.create_server(smtpd_factory, **create_server_kwargs)
-    server = event_loop.run_until_complete(server_coro)
+    event_loop = asyncio.get_running_loop()
+    server = await event_loop.create_server(smtpd_factory, **create_server_kwargs)
 
     yield server
 
     server.close()
     try:
-        event_loop.run_until_complete(cleanup_server(server))
+        await cleanup_server(server)
     except RuntimeError:
         pass
 
 
-@pytest.fixture(scope="function")
-def echo_server(
-    event_loop: asyncio.AbstractEventLoop, bind_address: str
-) -> Generator[asyncio.AbstractServer, None, None]:
-    server_coro = event_loop.create_server(
+@pytest_asyncio.fixture(scope="function")
+async def echo_server(bind_address: str) -> AsyncGenerator[asyncio.AbstractServer]:
+    event_loop = asyncio.get_running_loop()
+    server = await event_loop.create_server(
         EchoServerProtocol, host=bind_address, port=0, family=socket.AF_INET
     )
-    server = event_loop.run_until_complete(server_coro)
 
     yield server
 
     server.close()
     try:
-        event_loop.run_until_complete(cleanup_server(server))
+        await cleanup_server(server)
     except RuntimeError:
         pass
 
 
-@pytest.fixture(scope="function")
-def smtpd_server_socket_path(
+@pytest_asyncio.fixture(scope="function")
+async def smtpd_server_socket_path(
     request: pytest.FixtureRequest,
-    event_loop: asyncio.AbstractEventLoop,
     socket_path: Union[str, bytes, Path],
     server_tls_context: ssl.SSLContext,
     smtpd_factory: Callable[[], SMTPD],
-) -> Generator[asyncio.AbstractServer, None, None]:
+) -> AsyncGenerator[asyncio.AbstractServer]:
     smtpd_options_marker = request.node.get_closest_marker("smtpd_options")
     if smtpd_options_marker is None:
         smtpd_options = {}
     else:
         smtpd_options = smtpd_options_marker.kwargs
 
-    create_server_coro = event_loop.create_unix_server(
+    event_loop = asyncio.get_running_loop()
+    server = await event_loop.create_unix_server(
         smtpd_factory,
         path=socket_path,  # type: ignore
         ssl=server_tls_context if smtpd_options.get("tls", False) else None,
     )
-    server = event_loop.run_until_complete(create_server_coro)
 
     yield server
 
     server.close()
     try:
-        event_loop.run_until_complete(cleanup_server(server))
+        await cleanup_server(server)
     except RuntimeError:
         pass
 
@@ -568,13 +563,13 @@ def smtpd_server_threaded(smtpd_controller: SMTPDController) -> asyncio.Abstract
 # Running server ports #
 
 
-@pytest.fixture(scope="function")
-def smtpd_server_port(smtpd_server: asyncio.Server) -> int:
+@pytest_asyncio.fixture(scope="function")
+async def smtpd_server_port(smtpd_server: asyncio.Server) -> int:
     return int(smtpd_server.sockets[0].getsockname()[1])
 
 
-@pytest.fixture(scope="function")
-def echo_server_port(echo_server: asyncio.Server) -> int:
+@pytest_asyncio.fixture(scope="function")
+async def echo_server_port(echo_server: asyncio.Server) -> int:
     return int(echo_server.sockets[0].getsockname()[1])
 
 
@@ -587,8 +582,8 @@ def smtpd_server_threaded_port(smtpd_controller: SMTPDController) -> int:
 # SMTP Clients #
 
 
-@pytest.fixture(scope="function")
-def smtp_client(
+@pytest_asyncio.fixture(scope="function")
+async def smtp_client(
     request: pytest.FixtureRequest,
     hostname: str,
     smtpd_server_port: int,
@@ -611,8 +606,8 @@ def smtp_client(
     )
 
 
-@pytest.fixture(scope="function")
-def smtp_client_threaded(
+@pytest_asyncio.fixture(scope="function")
+async def smtp_client_threaded(
     hostname: str, smtpd_server_threaded_port: int, client_tls_context: ssl.SSLContext
 ) -> SMTP:
     return SMTP(
diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py
index b92f96f..dea11ca 100644
--- a/tests/test_asyncio.py
+++ b/tests/test_asyncio.py
@@ -165,12 +165,11 @@ async def test_many_commands_with_gather(smtp_client: SMTP) -> None:
     assert "Supported commands" in results[-1]
 
 
-async def test_close_works_on_stopped_loop(
+async def test_close_works_on_closing_transport(
     hostname: str,
     smtpd_server_port: int,
     client_tls_context: ssl.SSLContext,
 ) -> None:
-    event_loop = asyncio.get_running_loop()
     client = SMTP(
         hostname=hostname, port=smtpd_server_port, tls_context=client_tls_context
     )
@@ -179,7 +178,10 @@ async def test_close_works_on_stopped_loop(
     assert client.is_connected
     assert client.transport is not None
 
-    event_loop.stop()
+    # Ideally we'd test with a stopped event loop, but that breaks
+    # pytest-asyncio's finalizer.  Closing the client's transport is the
+    # next best thing.
+    client.transport.close()
 
     client.close()
     assert not client.is_connected
diff --git a/tests/test_protocol.py b/tests/test_protocol.py
index 56852e2..b61f45d 100644
--- a/tests/test_protocol.py
+++ b/tests/test_protocol.py
@@ -414,9 +414,9 @@ async def test_flow_control_mixin_drain_incomplete():
     assert not waiter.cancelled()
 
 
-def test_flow_control_mixin_connection_lost_exception(
-    event_loop: asyncio.AbstractEventLoop,
-) -> None:
+async def test_flow_control_mixin_connection_lost_exception() -> None:
+    event_loop = asyncio.get_running_loop()
+
     flow_control = FlowControlMixin(event_loop)
     flow_control.pause_writing()
     waiter = event_loop.create_future()
@@ -431,9 +431,9 @@ def test_flow_control_mixin_connection_lost_exception(
     assert waiter.exception() is exc
 
 
-def test_flow_control_mixin_connection_lost_no_exception(
-    event_loop: asyncio.AbstractEventLoop,
-) -> None:
+async def test_flow_control_mixin_connection_lost_no_exception() -> None:
+    event_loop = asyncio.get_running_loop()
+
     flow_control = FlowControlMixin(event_loop)
     flow_control.pause_writing()
     waiter = event_loop.create_future()
@@ -447,9 +447,9 @@ def test_flow_control_mixin_connection_lost_no_exception(
     assert waiter.exception() is None
 
 
-def test_flow_control_mixin_connection_lost_done(
-    event_loop: asyncio.AbstractEventLoop,
-) -> None:
+async def test_flow_control_mixin_connection_lost_done() -> None:
+    event_loop = asyncio.get_running_loop()
+
     flow_control = FlowControlMixin(event_loop)
     flow_control.pause_writing()
     waiter = event_loop.create_future()