Skip to content

Commit

Permalink
Fix pytest 8.4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
youtux committed Jan 11, 2025
1 parent ca52a05 commit 1d468a0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
13 changes: 12 additions & 1 deletion pytest_factoryboy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from collections.abc import Sequence
from importlib.metadata import version
from typing import TypeAlias

from _pytest.fixtures import FixtureDef, FixtureManager
from _pytest.nodes import Node
from packaging.version import parse as parse_version

pytest_version = parse_version(version("pytest"))

__all__ = ("PostGenerationContext", "getfixturedefs")
__all__ = ("PostGenerationContext", "getfixturedefs", "PytestFixtureT")

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

def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Sequence[FixtureDef] | None:
return fixturemanager.getfixturedefs(fixturename, node.nodeid)


if pytest_version.release >= (8, 4):
from _pytest.fixtures import FixtureFunctionDefinition

PytestFixtureT: TypeAlias = FixtureFunctionDefinition
else:
from _pytest.fixtures import FixtureFunction

PytestFixtureT: TypeAlias = FixtureFunction
6 changes: 3 additions & 3 deletions pytest_factoryboy/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions pytest_factoryboy/fixturegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import pytest
from typing_extensions import ParamSpec

from .compat import PytestFixtureT

T = TypeVar("T")
P = ParamSpec("P")

Expand All @@ -16,13 +18,13 @@ def create_fixture(
name: str,
function: Callable[P, T],
fixtures: Collection[str] | None = None,
) -> Callable[P, T]:
) -> tuple[PytestFixtureT, 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:
Expand All @@ -41,13 +43,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]]:
Expand Down

0 comments on commit 1d468a0

Please sign in to comment.