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

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

Closed
blueyed opened this issue Apr 7, 2019 · 5 comments
Closed
Labels
plugin: doctests related to the doctests builtin plugin topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed

Comments

@blueyed
Copy link
Contributor

blueyed commented Apr 7, 2019

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?

@Zac-HD Zac-HD added plugin: doctests related to the doctests builtin plugin topic: fixtures anything involving fixtures directly or indirectly type: enhancement new feature or API change, should be merged into features branch labels Apr 12, 2019
@blueyed
Copy link
Contributor Author

blueyed commented Nov 9, 2019

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?

@blueyed blueyed added topic: collection related to the collection phase type: bug problem that needs to be addressed and removed type: enhancement new feature or API change, should be merged into features branch labels Nov 9, 2019
@RonnyPfannschmidt
Copy link
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

@nicoddemus
Copy link
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. :/

@blueyed
Copy link
Contributor Author

blueyed commented Nov 13, 2019

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

@nicoddemus
Copy link
Member

nicoddemus commented Nov 13, 2019

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
Labels
plugin: doctests related to the doctests builtin plugin topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

4 participants