From 85c1776f71f7fb9cde87c61f0de8d2e9b7a8ebb6 Mon Sep 17 00:00:00 2001 From: Alessio Bogon <778703+youtux@users.noreply.github.com> Date: Sun, 12 Jan 2025 00:22:57 +0100 Subject: [PATCH] Fix pytest 8.4 compatibility --- pytest_factoryboy/fixture.py | 6 +++--- pytest_factoryboy/fixturegen.py | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pytest_factoryboy/fixture.py b/pytest_factoryboy/fixture.py index 8ef280c..e94b039 100644 --- a/pytest_factoryboy/fixture.py +++ b/pytest_factoryboy/fixture.py @@ -182,12 +182,12 @@ def create_fixture_with_related( ) -> Callable[P, T]: if related is None: related = [] - f = create_fixture(name=name, function=function, fixtures=fixtures) + fixture, fn = create_fixture(name=name, function=function, fixtures=fixtures) # We have to set the `_factoryboy_related` attribute to the original function, since # FixtureDef.func will provide that one later when we discover the related fixtures. - f.__pytest_wrapped__.obj._factoryboy_related = related # type: ignore[attr-defined] - return f + fn._factoryboy_related = related # type: ignore[attr-defined] + return fixture def make_declaration_fixturedef( diff --git a/pytest_factoryboy/fixturegen.py b/pytest_factoryboy/fixturegen.py index 55e9cca..df15e93 100644 --- a/pytest_factoryboy/fixturegen.py +++ b/pytest_factoryboy/fixturegen.py @@ -6,6 +6,7 @@ from typing import Callable, TypeVar import pytest +from _pytest.fixtures import FixtureFunctionDefinition from typing_extensions import ParamSpec T = TypeVar("T") @@ -16,13 +17,13 @@ def create_fixture( name: str, function: Callable[P, T], fixtures: Collection[str] | None = None, -) -> Callable[P, T]: +) -> tuple[FixtureFunctionDefinition, Callable[P, T]]: """Dynamically create a pytest fixture. :param name: Name of the fixture. :param function: Function to be called. :param fixtures: List of fixtures dependencies, but that will not be passed to ``function``. - :return: The created fixture function. + :return: The created fixture function and the actual function. Example: @@ -41,13 +42,14 @@ def book(name, db): if fixtures is None: fixtures = [] - @pytest.fixture(name=name) @usefixtures(*fixtures) @functools.wraps(function) def fn(*args: P.args, **kwargs: P.kwargs) -> T: return function(*args, **kwargs) - return fn + fixture = pytest.fixture(name=name, fixture_function=fn) + + return fixture, fn def usefixtures(*fixtures: str) -> Callable[[Callable[P, T]], Callable[P, T]]: