Skip to content

Commit 15cd119

Browse files
committed
Fix pytest 8.4 compatibility
1 parent ca52a05 commit 15cd119

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

pytest_factoryboy/compat.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from _pytest.fixtures import FixtureDef, FixtureManager
77
from _pytest.nodes import Node
88
from packaging.version import parse as parse_version
9+
from typing_extensions import TypeAlias
910

1011
pytest_version = parse_version(version("pytest"))
1112

12-
__all__ = ("PostGenerationContext", "getfixturedefs")
13+
__all__ = ("PostGenerationContext", "getfixturedefs", "PytestFixtureT")
1314

1415
try:
1516
from factory.declarations import PostGenerationContext
@@ -25,3 +26,13 @@ def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node)
2526

2627
def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Sequence[FixtureDef] | None:
2728
return fixturemanager.getfixturedefs(fixturename, node.nodeid)
29+
30+
31+
if pytest_version.release >= (8, 4):
32+
from _pytest.fixtures import FixtureFunctionDefinition
33+
34+
PytestFixtureT: TypeAlias = FixtureFunctionDefinition
35+
else:
36+
from _pytest.fixtures import FixtureFunction
37+
38+
PytestFixtureT: TypeAlias = FixtureFunction

pytest_factoryboy/fixture.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ def create_fixture_with_related(
182182
) -> Callable[P, T]:
183183
if related is None:
184184
related = []
185-
f = create_fixture(name=name, function=function, fixtures=fixtures)
185+
fixture, fn = create_fixture(name=name, function=function, fixtures=fixtures)
186186

187187
# We have to set the `_factoryboy_related` attribute to the original function, since
188188
# FixtureDef.func will provide that one later when we discover the related fixtures.
189-
f.__pytest_wrapped__.obj._factoryboy_related = related # type: ignore[attr-defined]
190-
return f
189+
fn._factoryboy_related = related # type: ignore[attr-defined]
190+
return fixture
191191

192192

193193
def make_declaration_fixturedef(

pytest_factoryboy/fixturegen.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import pytest
99
from typing_extensions import ParamSpec
1010

11+
from .compat import PytestFixtureT
12+
1113
T = TypeVar("T")
1214
P = ParamSpec("P")
1315

@@ -16,13 +18,13 @@ def create_fixture(
1618
name: str,
1719
function: Callable[P, T],
1820
fixtures: Collection[str] | None = None,
19-
) -> Callable[P, T]:
21+
) -> tuple[PytestFixtureT, Callable[P, T]]:
2022
"""Dynamically create a pytest fixture.
2123
2224
:param name: Name of the fixture.
2325
:param function: Function to be called.
2426
:param fixtures: List of fixtures dependencies, but that will not be passed to ``function``.
25-
:return: The created fixture function.
27+
:return: The created fixture function and the actual function.
2628
2729
Example:
2830
@@ -41,13 +43,14 @@ def book(name, db):
4143
if fixtures is None:
4244
fixtures = []
4345

44-
@pytest.fixture(name=name)
4546
@usefixtures(*fixtures)
4647
@functools.wraps(function)
4748
def fn(*args: P.args, **kwargs: P.kwargs) -> T:
4849
return function(*args, **kwargs)
4950

50-
return fn
51+
fixture = pytest.fixture(name=name, fixture_function=fn)
52+
53+
return fixture, fn
5154

5255

5356
def usefixtures(*fixtures: str) -> Callable[[Callable[P, T]], Callable[P, T]]:

0 commit comments

Comments
 (0)