diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 51be84164..f7e47cece 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -376,6 +376,9 @@ def _in_venv(path: Path) -> bool: def pytest_ignore_collect(collection_path: Path, config: Config) -> Optional[bool]: + if collection_path.name == "__pycache__": + return True + ignore_paths = config._getconftest_pathlist( "collect_ignore", path=collection_path.parent ) @@ -505,8 +508,6 @@ class Dir(nodes.Directory): ihook = self.ihook for direntry in scandir(self.path): if direntry.is_dir(): - if direntry.name == "__pycache__": - continue path = Path(direntry.path) if not self.session.isinitpath(path, with_parents=True): if ihook.pytest_ignore_collect(collection_path=path, config=config): diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 4ca77fbfa..1d7f0fd6f 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -707,8 +707,6 @@ class Package(nodes.Directory): ihook = self.ihook for direntry in scandir(self.path, sort_key): if direntry.is_dir(): - if direntry.name == "__pycache__": - continue path = Path(direntry.path) if not self.session.isinitpath(path, with_parents=True): if ihook.pytest_ignore_collect(collection_path=path, config=config):