From 2413d1b214dd2f58d301043dada54ff153ac373a Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 14 Jan 2024 15:05:15 +0200 Subject: [PATCH] main,python: move `__pycache__` ignore to `pytest_ignore_collect` This removes one thing that directory collectors need to worry about. This adds one hook dispatch per `__pycache__` file, but I think it's worth it for consistency. --- src/_pytest/main.py | 5 +++-- src/_pytest/python.py | 2 -- 2 files changed, 3 insertions(+), 4 deletions(-) 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):