Skip to content

Improve handling of task_files. #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
- {pull}`555` uses new-style hook wrappers and requires pluggy 1.3 for typing.
- {pull}`557` fixes an issue with `@task(after=...)` in notebooks and terminals.
- {pull}`566` makes universal-pathlib an official dependency.
- {pull}`568` restricts `task_files` to a list of patterns and raises a better error.

## 0.4.5 - 2024-01-09

Expand Down
2 changes: 1 addition & 1 deletion docs/source/reference_guides/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ strict_markers = true
Change the pattern which identify task files.

```toml
task_files = "task_*.py" # default
task_files = ["task_*.py"] # default

task_files = ["task_*.py", "tasks_*.py"]
```
Expand Down
2 changes: 1 addition & 1 deletion src/_pytask/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def build( # noqa: C901, PLR0912, PLR0913, PLR0915
stop_after_first_failure: bool = False,
strict_markers: bool = False,
tasks: Callable[..., Any] | PTask | Iterable[Callable[..., Any] | PTask] = (),
task_files: str | Iterable[str] = "task_*.py",
task_files: Iterable[str] = ("task_*.py",),
trace: bool = False,
verbose: int = 1,
**kwargs: Any,
Expand Down
8 changes: 7 additions & 1 deletion src/_pytask/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ def pytask_parse_config(config: dict[str, Any]) -> None:
+ IGNORED_TEMPORARY_FILES_AND_FOLDERS
)

config["task_files"] = to_list(config.get("task_files", "task_*.py"))
value = config.get("task_files", ["task_*.py"])
if not isinstance(value, (list, tuple)) or not all(
isinstance(p, str) for p in value
):
msg = "'task_files' must be a list of patterns."
raise ValueError(msg)
config["task_files"] = value

if config["stop_after_first_failure"]:
config["max_failures"] = 1
Expand Down
27 changes: 21 additions & 6 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def test_collect_same_task_different_ways(tmp_path, path_extension):
@pytest.mark.parametrize(
("task_files", "pattern", "expected_collected_tasks"),
[
(["example_task.py"], "'*_task.py'", 1),
(["tasks_example.py"], "'tasks_*'", 1),
(["example_tasks.py"], "'*_tasks.py'", 1),
(["task_module.py", "tasks_example.py"], "'tasks_*.py'", 1),
(["example_task.py"], "['*_task.py']", 1),
(["tasks_example.py"], "['tasks_*']", 1),
(["example_tasks.py"], "['*_tasks.py']", 1),
(["task_module.py", "tasks_example.py"], "['tasks_*.py']", 1),
(["task_module.py", "tasks_example.py"], "['task_*.py', 'tasks_*.py']", 2),
],
)
Expand All @@ -117,15 +117,30 @@ def test_collect_files_w_custom_file_name_pattern(
f"[tool.pytask.ini_options]\ntask_files = {pattern}"
)

for file in task_files:
tmp_path.joinpath(file).write_text("def task_example(): pass")
for file_ in task_files:
tmp_path.joinpath(file_).write_text("def task_example(): pass")

session = build(paths=tmp_path)

assert session.exit_code == ExitCode.OK
assert len(session.tasks) == expected_collected_tasks


def test_error_with_invalid_file_name_pattern(runner, tmp_path):
tmp_path.joinpath("pyproject.toml").write_text(
"[tool.pytask.ini_options]\ntask_files = 'asds'"
)

result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.CONFIGURATION_FAILED
assert "'task_files' must be a list of patterns." in result.output


def test_error_with_invalid_file_name_pattern_(tmp_path):
session = build(paths=tmp_path, task_files=[1])
assert session.exit_code == ExitCode.CONFIGURATION_FAILED


@pytest.mark.unit()
@pytest.mark.parametrize(
("session", "path", "node_info", "expected"),
Expand Down