diff --git a/changelog/3771.bugfix.rst b/changelog/3771.bugfix.rst new file mode 100644 index 000000000..09c953aa2 --- /dev/null +++ b/changelog/3771.bugfix.rst @@ -0,0 +1 @@ +Fix infinite recursion during collection if a ``pytest_ignore_collect`` returns ``False`` instead of ``None``. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 5b8305e77..2657bff63 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -561,7 +561,7 @@ class Package(Module): def _recurse(self, path): ihook = self.gethookproxy(path.dirpath()) if ihook.pytest_ignore_collect(path=path, config=self.config): - return + return False for pat in self._norecursepatterns: if path.check(fnmatch=pat): return False @@ -594,9 +594,12 @@ class Package(Module): return path in self.session._initialpaths def collect(self): - path = self.fspath.dirpath() + this_path = self.fspath.dirpath() pkg_prefix = None - for path in path.visit(fil=lambda x: 1, rec=self._recurse, bf=True, sort=True): + for path in this_path.visit(rec=self._recurse, bf=True, sort=True): + # we will visit our own __init__.py file, in which case we skip it + if path.basename == "__init__.py" and path.dirpath() == this_path: + continue if pkg_prefix and pkg_prefix in path.parts(): continue for x in self._collectfile(path):