Skip to content

Commit

Permalink
Extract getfixturedefs into compat.py
Browse files Browse the repository at this point in the history
  • Loading branch information
youtux committed Jan 21, 2024
1 parent a75c644 commit 97e5d1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 20 additions & 0 deletions pytest_factoryboy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

import pathlib
import sys
from collections.abc import Sequence
from importlib.metadata import version

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

pytest_version = parse_version(version("pytest"))

__all__ = ("PostGenerationContext", "path_with_stem")

Expand All @@ -19,3 +28,14 @@ def path_with_stem(path: pathlib.Path, stem: str) -> pathlib.Path:

def path_with_stem(path: pathlib.Path, stem: str) -> pathlib.Path:
return path.with_name(stem + path.suffix)


if pytest_version >= Version("8.1"):

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

else:

def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Sequence[FixtureDef] | None:
return fixturemanager.getfixturedefs(fixturename, node.nodeid)
7 changes: 3 additions & 4 deletions pytest_factoryboy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import pytest

from .compat import getfixturedefs

if TYPE_CHECKING:
from typing import Any

Expand Down Expand Up @@ -48,10 +50,7 @@ def get_deps(self, request: SubRequest, fixture: str, deps: set[str] | None = No
if fixture == "request":
return deps

if hasattr(pytest, "version_tuple") and pytest.version_tuple >= (8, 1):
fixturedefs = request._fixturemanager.getfixturedefs(fixture, request._pyfuncitem.parent)
else:
fixturedefs = request._fixturemanager.getfixturedefs(fixture, request._pyfuncitem.parent.nodeid)
fixturedefs = getfixturedefs(request._fixturemanager, fixture, request._pyfuncitem.parent)
for fixturedef in fixturedefs or []:
for argname in fixturedef.argnames:
if argname not in deps:
Expand Down

0 comments on commit 97e5d1b

Please sign in to comment.