diff --git a/CHANGES.rst b/CHANGES.rst index f1dab20..868cd45 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,7 @@ Changelog Unreleased ---------- +- Address compatibility issue with pytest 8.1. `#213 `_ 2.6.0 ---------- diff --git a/poetry.lock b/poetry.lock index 8d5f88f..e18fcda 100644 --- a/poetry.lock +++ b/poetry.lock @@ -536,4 +536,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.7" -content-hash = "cd68a5badaefeb63525aff0484fef3ce9e04938a1ac8ca2cc2a551a418841155" +content-hash = "4c163068d378da2bfd22b01b77ca67733f74339568625cdd0f91de132202c975" diff --git a/pyproject.toml b/pyproject.toml index 6364d19..264b386 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,7 @@ inflection = "*" factory_boy = ">=2.10.0" pytest = ">=6.2" typing_extensions = "*" +packaging = "*" [tool.poetry.group.dev.dependencies] mypy = ">=1.4.1" diff --git a/pytest_factoryboy/compat.py b/pytest_factoryboy/compat.py index a29dfd8..52ecc7e 100644 --- a/pytest_factoryboy/compat.py +++ b/pytest_factoryboy/compat.py @@ -2,8 +2,16 @@ import pathlib import sys +from collections.abc import Sequence +from importlib.metadata import version -__all__ = ("PostGenerationContext", "path_with_stem") +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", "path_with_stem", "getfixturedefs") try: from factory.declarations import PostGenerationContext @@ -19,3 +27,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.release >= (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) diff --git a/pytest_factoryboy/plugin.py b/pytest_factoryboy/plugin.py index 9f05e8f..610bc29 100644 --- a/pytest_factoryboy/plugin.py +++ b/pytest_factoryboy/plugin.py @@ -6,6 +6,8 @@ import pytest +from .compat import getfixturedefs + if TYPE_CHECKING: from typing import Any @@ -48,7 +50,8 @@ def get_deps(self, request: SubRequest, fixture: str, deps: set[str] | None = No if fixture == "request": return deps - for fixturedef in request._fixturemanager.getfixturedefs(fixture, request._pyfuncitem.parent.nodeid) or []: + fixturedefs = getfixturedefs(request._fixturemanager, fixture, request._pyfuncitem.parent) + for fixturedef in fixturedefs or []: for argname in fixturedef.argnames: if argname not in deps: deps.add(argname)