@@ -110,6 +110,7 @@ def fixture(
110
110
fixture_function : FixtureFunction ,
111
111
* ,
112
112
scope : "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
113
+ loop_scope : Union [_ScopeName ] = ...,
113
114
params : Optional [Iterable [object ]] = ...,
114
115
autouse : bool = ...,
115
116
ids : Union [
@@ -126,6 +127,7 @@ def fixture(
126
127
fixture_function : None = ...,
127
128
* ,
128
129
scope : "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
130
+ loop_scope : Union [_ScopeName , None ] = ...,
129
131
params : Optional [Iterable [object ]] = ...,
130
132
autouse : bool = ...,
131
133
ids : Union [
@@ -138,17 +140,19 @@ def fixture(
138
140
139
141
140
142
def fixture (
141
- fixture_function : Optional [FixtureFunction ] = None , ** kwargs : Any
143
+ fixture_function : Optional [FixtureFunction ] = None ,
144
+ loop_scope : Union [_ScopeName , None ] = None ,
145
+ ** kwargs : Any ,
142
146
) -> Union [FixtureFunction , FixtureFunctionMarker ]:
143
147
if fixture_function is not None :
144
- _make_asyncio_fixture_function (fixture_function )
148
+ _make_asyncio_fixture_function (fixture_function , loop_scope )
145
149
return pytest .fixture (fixture_function , ** kwargs )
146
150
147
151
else :
148
152
149
153
@functools .wraps (fixture )
150
154
def inner (fixture_function : FixtureFunction ) -> FixtureFunction :
151
- return fixture (fixture_function , ** kwargs )
155
+ return fixture (fixture_function , loop_scope = loop_scope , ** kwargs )
152
156
153
157
return inner
154
158
@@ -158,11 +162,14 @@ def _is_asyncio_fixture_function(obj: Any) -> bool:
158
162
return getattr (obj , "_force_asyncio_fixture" , False )
159
163
160
164
161
- def _make_asyncio_fixture_function (obj : Any ) -> None :
165
+ def _make_asyncio_fixture_function (
166
+ obj : Any , loop_scope : Union [_ScopeName , None ]
167
+ ) -> None :
162
168
if hasattr (obj , "__func__" ):
163
169
# instance method, check the function object
164
170
obj = obj .__func__
165
171
obj ._force_asyncio_fixture = True
172
+ obj ._loop_scope = loop_scope
166
173
167
174
168
175
def _is_coroutine_or_asyncgen (obj : Any ) -> bool :
@@ -218,7 +225,7 @@ def _preprocess_async_fixtures(
218
225
# Ignore async fixtures without explicit asyncio mark in strict mode
219
226
# This applies to pytest_trio fixtures, for example
220
227
continue
221
- scope = fixturedef .scope
228
+ scope = getattr ( func , "_loop_scope" , None ) or fixturedef .scope
222
229
if scope == "function" :
223
230
event_loop_fixture_id : Optional [str ] = "event_loop"
224
231
else :
@@ -228,7 +235,7 @@ def _preprocess_async_fixtures(
228
235
_event_loop_fixture_id , # type: ignore[arg-type]
229
236
None ,
230
237
)
231
- _make_asyncio_fixture_function (func )
238
+ _make_asyncio_fixture_function (func , scope )
232
239
function_signature = inspect .signature (func )
233
240
if "event_loop" in function_signature .parameters :
234
241
warnings .warn (
0 commit comments