Skip to content

Add warning when non-matching files are passed to pytask. #627

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 2 commits into from
Jul 12, 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
3 changes: 3 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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}`627` adds a warning when users explicitly pass files to pytask that pytask is
going to ignore because they do not match a pattern. Happens quite often when the task
module's name does not start with `task_`.

## 0.5.0 - 2024-05-26

Expand Down
14 changes: 14 additions & 0 deletions src/_pytask/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import TYPE_CHECKING
from typing import Any

from _pytask.console import console
from _pytask.pluginmanager import hookimpl
from _pytask.shared import parse_markers
from _pytask.shared import parse_paths
Expand Down Expand Up @@ -115,3 +116,16 @@ def pytask_parse_config(config: dict[str, Any]) -> None:
def pytask_post_parse(config: dict[str, Any]) -> None:
"""Sort markers alphabetically."""
config["markers"] = {k: config["markers"][k] for k in sorted(config["markers"])}

# Show a warning to the user if they passed a path pointing to a Python module that
# does not match the task file pattern.
for path in config["paths"]:
if path.is_file() and not any(
path.match(pattern) for pattern in config["task_files"]
):
msg = (
f"Warning: The path '{path}' does not match any of the task file "
f"patterns in {config['task_files']}. Rename the file or configure a "
"different 'task_files' pattern if you want to collect it."
)
console.print(msg, style="warning")
14 changes: 14 additions & 0 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,17 @@ def task_example() -> Annotated[str, Path("file.txt")]: ...
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.COLLECTION_FAILED
assert "The task uses multiple ways to parse" in result.output


@pytest.mark.end_to_end()
def test_print_warning_if_non_matching_path_is_passed(runner, tmp_path):
tmp_path.joinpath("task.py").write_text("def task_example(): pass")
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "Collected 0 tasks" in result.output
assert "Warning: The path" not in result.output

result = runner.invoke(cli, [tmp_path.joinpath("task.py").as_posix()])
assert result.exit_code == ExitCode.OK
assert "Collected 0 tasks" in result.output
assert "Warning: The path" in result.output
Loading