diff --git a/src/_pytest/main.py b/src/_pytest/main.py index dbe1ccf42..9a0162f06 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -533,17 +533,20 @@ class Session(nodes.FSCollector): fil=filter_, rec=self._recurse, bf=True, sort=True ): pkginit = path.dirpath().join("__init__.py") - if pkginit.exists() and not any(x in parts(pkginit.strpath) for x in paths): + if pkginit.exists() and not any( + x in parts(pkginit.strpath) for x in paths + ): for x in root._collectfile(pkginit): yield x paths.append(x.fspath.dirpath()) if not any(x in parts(path.strpath) for x in paths): for x in root._collectfile(path): - if (type(x), x.fspath) in self._node_cache: - yield self._node_cache[(type(x), x.fspath)] + key = (type(x), x.fspath) + if key in self._node_cache: + yield self._node_cache[key] else: - self._node_cache[(type(x), x.fspath)] = x + self._node_cache[key] = x yield x else: assert argpath.check(file=1) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 2eb5f0b87..6b113cacd 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -563,6 +563,10 @@ class Package(Module): yield Module(init_module, self) pkg_prefixes = set() 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.isfile(): + if path.basename == "__init__.py" and path.dirpath() == this_path: + continue parts_ = parts(path.strpath) if any(