Skip to content

Commit b47cfa4

Browse files
committed
refactor: Replace custom fixture func rebinding with pytest function
1 parent b3e2166 commit b47cfa4

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

pytest_asyncio/plugin.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import pluggy
3535
import pytest
36+
from _pytest.fixtures import resolve_fixture_function
3637
from _pytest.scope import Scope
3738
from pytest import (
3839
Config,
@@ -60,7 +61,6 @@
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")
6666
FixtureFunction = 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-
275262
AsyncGenFixtureParams = ParamSpec("AsyncGenFixtureParams")
276263
AsyncGenFixtureYieldType = 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

Comments
 (0)