3333
3434import pluggy
3535import pytest
36+ from _pytest .fixtures import resolve_fixture_function
3637from _pytest .scope import Scope
3738from pytest import (
3839 Config ,
6061 from backports .asyncio .runner import Runner
6162
6263_ScopeName = Literal ["session" , "package" , "module" , "class" , "function" ]
63- _T = TypeVar ("_T" )
6464_R = TypeVar ("_R" , bound = Union [Awaitable [Any ], AsyncIterator [Any ]])
6565_P = ParamSpec ("_P" )
6666FixtureFunction = Callable [_P , _R ]
@@ -234,12 +234,15 @@ def pytest_report_header(config: Config) -> list[str]:
234234 ]
235235
236236
237- def _fixture_synchronizer (fixturedef : FixtureDef , runner : Runner ) -> Callable :
237+ def _fixture_synchronizer (
238+ fixturedef : FixtureDef , runner : Runner , request : FixtureRequest
239+ ) -> Callable :
238240 """Returns a synchronous function evaluating the specified fixture."""
241+ fixture_function = resolve_fixture_function (fixturedef , request )
239242 if inspect .isasyncgenfunction (fixturedef .func ):
240- return _wrap_asyncgen_fixture (fixturedef . func , runner )
243+ return _wrap_asyncgen_fixture (fixture_function , runner ) # type: ignore[arg-type]
241244 elif inspect .iscoroutinefunction (fixturedef .func ):
242- return _wrap_async_fixture (fixturedef . func , runner )
245+ return _wrap_async_fixture (fixture_function , runner ) # type: ignore[arg-type]
243246 else :
244247 return fixturedef .func
245248
@@ -256,22 +259,6 @@ def _add_kwargs(
256259 return ret
257260
258261
259- def _perhaps_rebind_fixture_func (func : _T , instance : Any | None ) -> _T :
260- if instance is not None :
261- # The fixture needs to be bound to the actual request.instance
262- # so it is bound to the same object as the test method.
263- unbound , cls = func , None
264- try :
265- unbound , cls = func .__func__ , type (func .__self__ ) # type: ignore
266- except AttributeError :
267- pass
268- # Only if the fixture was bound before to an instance of
269- # the same type.
270- if cls is not None and isinstance (instance , cls ):
271- func = unbound .__get__ (instance ) # type: ignore
272- return func
273-
274-
275262AsyncGenFixtureParams = ParamSpec ("AsyncGenFixtureParams" )
276263AsyncGenFixtureYieldType = TypeVar ("AsyncGenFixtureYieldType" )
277264
@@ -290,8 +277,9 @@ def _asyncgen_fixture_wrapper(
290277 * args : AsyncGenFixtureParams .args ,
291278 ** kwargs : AsyncGenFixtureParams .kwargs ,
292279 ):
293- func = _perhaps_rebind_fixture_func (fixture_function , request .instance )
294- gen_obj = func (* args , ** _add_kwargs (func , kwargs , request ))
280+ gen_obj = fixture_function (
281+ * args , ** _add_kwargs (fixture_function , kwargs , request )
282+ )
295283
296284 async def setup ():
297285 res = await gen_obj .__anext__ () # type: ignore[union-attr]
@@ -342,10 +330,10 @@ def _async_fixture_wrapper(
342330 * args : AsyncFixtureParams .args ,
343331 ** kwargs : AsyncFixtureParams .kwargs ,
344332 ):
345- func = _perhaps_rebind_fixture_func (fixture_function , request .instance )
346-
347333 async def setup ():
348- res = await func (* args , ** _add_kwargs (func , kwargs , request ))
334+ res = await fixture_function (
335+ * args , ** _add_kwargs (fixture_function , kwargs , request )
336+ )
349337 return res
350338
351339 context = contextvars .copy_context ()
@@ -746,7 +734,7 @@ def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None:
746734 )
747735 runner_fixture_id = f"_{ loop_scope } _scoped_runner"
748736 runner = request .getfixturevalue (runner_fixture_id )
749- synchronizer = _fixture_synchronizer (fixturedef , runner )
737+ synchronizer = _fixture_synchronizer (fixturedef , runner , request )
750738 _make_asyncio_fixture_function (synchronizer , loop_scope )
751739 with MonkeyPatch .context () as c :
752740 if "request" not in fixturedef .argnames :
0 commit comments