Skip to content

Commit a5daa93

Browse files
tobiasraabetimmens
andauthored
Fix duplicated collection of task modules. (#628)
Co-authored-by: Tim Mensinger <[email protected]>
1 parent e0056c8 commit a5daa93

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

docs/source/changes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
1616
- {pull}`627` adds a warning when users explicitly pass files to pytask that pytask is
1717
going to ignore because they do not match a pattern. Happens quite often when the task
1818
module's name does not start with `task_`.
19+
- {pull}`628` fixes duplicated collection of task modules. Fixes {issue}`624`. Thanks to
20+
{user}`timmens` for the issue.
1921

2022
## 0.5.0 - 2024-05-26
2123

src/_pytask/collect.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _collect_from_paths(session: Session) -> None:
9494
Go through all paths, check if the path is ignored, and collect the file if not.
9595
9696
"""
97-
for path in _not_ignored_paths(session.config["paths"], session):
97+
for path in _not_ignored_paths(session.config["paths"], session, set()):
9898
reports = session.hook.pytask_collect_file_protocol(
9999
session=session, path=path, reports=session.collection_reports
100100
)
@@ -519,7 +519,7 @@ def _raise_error_if_casing_of_path_is_wrong(
519519

520520

521521
def _not_ignored_paths(
522-
paths: Iterable[Path], session: Session
522+
paths: Iterable[Path], session: Session, seen: set[Path]
523523
) -> Generator[Path, None, None]:
524524
"""Traverse paths and yield not ignored paths.
525525
@@ -532,8 +532,9 @@ def _not_ignored_paths(
532532
if not session.hook.pytask_ignore_collect(path=path, config=session.config):
533533
if path.is_dir():
534534
files_in_dir = path.iterdir()
535-
yield from _not_ignored_paths(files_in_dir, session)
536-
else:
535+
yield from _not_ignored_paths(files_in_dir, session, seen)
536+
elif path not in seen:
537+
seen.add(path)
537538
yield path
538539

539540

tests/test_collect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def test_collect_same_task_different_ways(tmp_path, path_extension):
9999
assert len(session.tasks) == 1
100100

101101

102+
def test_modules_are_not_collected_twice(runner, tmp_path):
103+
"""See #624."""
104+
tmp_path.joinpath("task_module.py").write_text("def task_example(): pass")
105+
tmp_path.joinpath("pyproject.toml").write_text(
106+
"[tool.pytask.ini_options]\npaths = ['.', '.']"
107+
)
108+
result = runner.invoke(cli, [tmp_path.as_posix()])
109+
assert "Collected 1 task" in result.output
110+
111+
102112
@pytest.mark.end_to_end()
103113
@pytest.mark.parametrize(
104114
("task_files", "pattern", "expected_collected_tasks"),

0 commit comments

Comments
 (0)