From 4366230a8a9116ed502fdac12a3263bb2c247428 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Fri, 12 Jul 2024 22:10:12 +0200 Subject: [PATCH 1/2] Add test. --- tests/test_collect.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_collect.py b/tests/test_collect.py index 935919981..e3ac2ecf6 100644 --- a/tests/test_collect.py +++ b/tests/test_collect.py @@ -99,6 +99,16 @@ def test_collect_same_task_different_ways(tmp_path, path_extension): assert len(session.tasks) == 1 +def test_modules_are_not_collected_twice(runner, tmp_path): + """See #624.""" + tmp_path.joinpath("task_module.py").write_text("def task_example(): pass") + tmp_path.joinpath("pyproject.toml").write_text( + "[tool.pytask.ini_options]\npaths = ['.', '.']" + ) + result = runner.invoke(cli, [tmp_path.as_posix()]) + assert "Collected 1 task" in result.output + + @pytest.mark.end_to_end() @pytest.mark.parametrize( ("task_files", "pattern", "expected_collected_tasks"), From d543df65f10f3850d5e57d8876715e443860e51b Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Fri, 12 Jul 2024 22:12:02 +0200 Subject: [PATCH 2/2] Fix. --- docs/source/changes.md | 2 ++ src/_pytask/collect.py | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/source/changes.md b/docs/source/changes.md index d4ce3a5d3..ff5b09010 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -13,6 +13,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and - {pull}`619` makes coiled an optional import for tests. Thanks to {user}`erooke`. - {pull}`620` makes tests more flexible about their location. Thanks to {user}`erooke`. - {pull}`621` fixes the pull requests template. +- {pull}`628` fixes duplicated collection of task modules. Fixes {issue}`624`. Thanks to + {user}`timmens` for the issue. ## 0.5.0 - 2024-05-26 diff --git a/src/_pytask/collect.py b/src/_pytask/collect.py index 994279273..c63889e06 100644 --- a/src/_pytask/collect.py +++ b/src/_pytask/collect.py @@ -94,7 +94,7 @@ def _collect_from_paths(session: Session) -> None: Go through all paths, check if the path is ignored, and collect the file if not. """ - for path in _not_ignored_paths(session.config["paths"], session): + for path in _not_ignored_paths(session.config["paths"], session, set()): reports = session.hook.pytask_collect_file_protocol( session=session, path=path, reports=session.collection_reports ) @@ -520,7 +520,7 @@ def _raise_error_if_casing_of_path_is_wrong( def _not_ignored_paths( - paths: Iterable[Path], session: Session + paths: Iterable[Path], session: Session, seen: set[Path] ) -> Generator[Path, None, None]: """Traverse paths and yield not ignored paths. @@ -533,8 +533,9 @@ def _not_ignored_paths( if not session.hook.pytask_ignore_collect(path=path, config=session.config): if path.is_dir(): files_in_dir = path.iterdir() - yield from _not_ignored_paths(files_in_dir, session) - else: + yield from _not_ignored_paths(files_in_dir, session, seen) + elif path not in seen: + seen.add(path) yield path