Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pytest 8.4 compatibility #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pytest_factoryboy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from _pytest.fixtures import FixtureDef, FixtureManager
from _pytest.nodes import Node
from packaging.version import parse as parse_version
from typing_extensions import TypeAlias

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
Loading