Skip to content

[bug] pytest_collect_file gets called multiple times #6088

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ def __init__(self, config):

self.config.pluginmanager.register(self, name="session")

self._collected = set()

def __repr__(self):
return "<%s %s exitstatus=%r testsfailed=%d testscollected=%d>" % (
self.__class__.__name__,
Expand Down Expand Up @@ -575,6 +577,10 @@ def _collect(self, arg):
yield from m

def _collectfile(self, path, handle_dupes=True):
key = (path, handle_dupes)
assert key not in self._collected, key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not familiar with the code, how does this interact with handle_dupes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle_duples is also a hack on top (IIRC even by me already). I just added it here. The code is not meant to stay.
_collectfile could use a cache/lookup to work around this - the main issue is that hooks shouldn't really be called multiple times. I've only noticed this when debugging some --lf stuff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#4319 . i actually merged it

i believe this will b better if we shift packages around so they are parents of modules and not alongside of modules
(then we can also attach conftests to packages/blank packages and finally get rid of the nodeid string matching hell)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

self._collected.add(key)

assert (
path.isfile()
), "{!r} is not a file (isdir={!r}, exists={!r}, islink={!r})".format(
Expand Down
11 changes: 11 additions & 0 deletions testing/example_scripts/collect/collect_init_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import Set
from typing import Tuple

collected_already = set() # type: Set[Tuple]


def pytest_collect_file(path, parent):
# Ensure that the hook is only called once per path.
key = (path, parent)
assert key not in collected_already, key
collected_already.add(key)