pytest-fixture-tools icon indicating copy to clipboard operation
pytest-fixture-tools copied to clipboard

Fix getfixturedefs call

Open MaksimKravchuk opened this issue 1 year ago • 1 comments

Problem: Test runs with --show-fixture-duplicates fail with error:

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/user/Code/e2e/venv/lib/python3.12/site-packages/_pytest/fixtures.py", line 1771, in _matchfactories
INTERNALERROR>     parentnodeids = {n.nodeid for n in node.iter_parents()}
INTERNALERROR>                                        ^^^^^^^^^^^^^^^^^
INTERNALERROR> AttributeError: 'str' object has no attribute 'iter_parents'
INTERNALERROR> 
INTERNALERROR> During handling of the above exception, another exception occurred:
INTERNALERROR> 
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/user/Code/e2e/venv/lib/python3.12/site-packages/_pytest/main.py", line 285, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>                          ^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/Users/user/Code/e2e/venv/lib/python3.12/site-packages/pytest_fixture_tools/plugin.py", line 94, in _show_fixture_duplicates_main
INTERNALERROR>     fixturedefs = fm.getfixturedefs(argname, item.nodeid)
INTERNALERROR>                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/Users/user/Code/e2e/venv/lib/python3.12/site-packages/_pytest/fixtures.py", line 1765, in getfixturedefs
INTERNALERROR>     return tuple(self._matchfactories(fixturedefs, node))
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/Users/user/Code/e2e/venv/lib/python3.12/site-packages/_pytest/fixtures.py", line 1773, in _matchfactories
INTERNALERROR>     raise ValueError("node must be a Node instance")
INTERNALERROR> ValueError: node must be a Node instance

It happens because getfixturedefs takes a Node object, not a node ID string. https://github.com/pytest-dev/pytest/blob/949c7711b3e22ab94110f1ae4fc307cda2f9e379/src/_pytest/fixtures.py#L1824

This PR fix the problem.


This change is Reviewable

MaksimKravchuk avatar Dec 11 '24 16:12 MaksimKravchuk

@ogajduse Hey! Please take a look at this PR

MaksimKravchuk avatar Feb 13 '25 20:02 MaksimKravchuk

It is indeed as you mentioned. The signature of the function is:

def getfixturedefs(
        self, argname: str, node: nodes.Node
    ) -> Sequence[FixtureDef[Any]] | None:

Docs: https://docs.pytest.org/en/8.3.x/_modules/_pytest/fixtures.html

ogajduse avatar Apr 30 '25 09:04 ogajduse

It took me a while, but a new release with your patch is out. https://pypi.org/project/pytest-fixture-tools/1.2.1/

ogajduse avatar Apr 30 '25 11:04 ogajduse