Skip to content

AttributeError: 'DoctestItem' object has no attribute 'fixturenames' #5070

Closed

Description

@blueyed

I think a common use case is to look at fixtures in pytest_collect_modifyitems:

def pytest_collection_modifyitems(config, items):
    for item in items:
        if "foo" in item.fixturenames:
…

But this crashes when doctests are involved:

  File "…/Vcs/pytest/src/_pytest/main.py", line 261, in pytest_collection
    return session.perform_collect()
  File "…/Vcs/pytest/src/_pytest/main.py", line 500, in perform_collect
    session=self, config=self.config, items=items
  File "…/Vcs/pluggy/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "…/Vcs/pluggy/pluggy/manager.py", line 68, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "…/Vcs/pluggy/pluggy/manager.py", line 62, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "…/Vcs/pluggy/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "…/Vcs/pluggy/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "…/Vcs/pluggy/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "…/Vcs/pytest/testing/conftest.py", line 6, in pytest_collection_modifyitems
    for item in items:
AttributeError: 'DoctestItem' object has no attribute 'fixturenames'

This was fixed for --fixtures-per-test in #2789, but I think it would be better to just have an empty list there maybe?

Activity

  1. added
    plugin: doctests
    topic: fixtures
    type: enhancement
    on Apr 12, 2019
  2. blueyed commented on Nov 9, 2019

    @blueyed
    ContributorAuthor

    Any pointers in how to address this?
    It's really unfortunate that every possible plugin has to handle/ignore that - especially since it might only happen then when using doctests (as within pytest's own tests).

    I think it should be a given that items ins pytest_collection_modifyitems have an fixturenames attribute, no?

  3. added
    topic: collection
    type: bug
    and removed
    type: enhancement
    on Nov 9, 2019
  4. RonnyPfannschmidt commented on Nov 10, 2019

    @RonnyPfannschmidt
    Member

    i believe it will happen to practically all item types that have no direct fixture system integration (aka 3rd party plugins as well, its really tricky to touch

  5. nicoddemus commented on Nov 13, 2019

    @nicoddemus
    Member

    I think it should be a given that items ins pytest_collection_modifyitems have an fixturenames attribute, no?

    Yeah as @RonnyPfannschmidt said, custom items which don't implement fixtures, while uncommon, are a definite possibility (besides Doctests, pytest-cpp also has custom items).

    Unfortunately I don't have any idea on how to improve this situation.

    Unless somebody has a suggestion, I think we should close this. :/

  6. blueyed commented on Nov 13, 2019

    @blueyed
    ContributorAuthor

    What about a mixin / base class to have an empty list there?

  7. nicoddemus commented on Nov 13, 2019

    @nicoddemus
    Member

    From a "pure" design POV it would not be nice, because items are generic and might be customizable. Fixtures are a feature only available on pytest.Function objects. On the other hand, from a practibility POV it might make sense.

    But I lean towards the former, otherwise we now have this implicit contract that pytest.Item needs to support all methods/attributes a pytest.Function does.

    I know it sucks and it trips some people, but down the road we might make things worse for ourselves if we have to guarantee that all items have a pytest.Function-like interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: docteststopic: collectiontopic: fixturestype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions