Skip to content

Commit e0056c8

Browse files
authored
Add warning when non-matching files are passed to pytask. (#627)
1 parent 396030c commit e0056c8

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

docs/source/changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
1313
- {pull}`619` makes coiled an optional import for tests. Thanks to {user}`erooke`.
1414
- {pull}`620` makes tests more flexible about their location. Thanks to {user}`erooke`.
1515
- {pull}`621` fixes the pull requests template.
16+
- {pull}`627` adds a warning when users explicitly pass files to pytask that pytask is
17+
going to ignore because they do not match a pattern. Happens quite often when the task
18+
module's name does not start with `task_`.
1619

1720
## 0.5.0 - 2024-05-26
1821

src/_pytask/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import TYPE_CHECKING
88
from typing import Any
99

10+
from _pytask.console import console
1011
from _pytask.pluginmanager import hookimpl
1112
from _pytask.shared import parse_markers
1213
from _pytask.shared import parse_paths
@@ -115,3 +116,16 @@ def pytask_parse_config(config: dict[str, Any]) -> None:
115116
def pytask_post_parse(config: dict[str, Any]) -> None:
116117
"""Sort markers alphabetically."""
117118
config["markers"] = {k: config["markers"][k] for k in sorted(config["markers"])}
119+
120+
# Show a warning to the user if they passed a path pointing to a Python module that
121+
# does not match the task file pattern.
122+
for path in config["paths"]:
123+
if path.is_file() and not any(
124+
path.match(pattern) for pattern in config["task_files"]
125+
):
126+
msg = (
127+
f"Warning: The path '{path}' does not match any of the task file "
128+
f"patterns in {config['task_files']}. Rename the file or configure a "
129+
"different 'task_files' pattern if you want to collect it."
130+
)
131+
console.print(msg, style="warning")

tests/test_collect.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,17 @@ def task_example() -> Annotated[str, Path("file.txt")]: ...
582582
result = runner.invoke(cli, [tmp_path.as_posix()])
583583
assert result.exit_code == ExitCode.COLLECTION_FAILED
584584
assert "The task uses multiple ways to parse" in result.output
585+
586+
587+
@pytest.mark.end_to_end()
588+
def test_print_warning_if_non_matching_path_is_passed(runner, tmp_path):
589+
tmp_path.joinpath("task.py").write_text("def task_example(): pass")
590+
result = runner.invoke(cli, [tmp_path.as_posix()])
591+
assert result.exit_code == ExitCode.OK
592+
assert "Collected 0 tasks" in result.output
593+
assert "Warning: The path" not in result.output
594+
595+
result = runner.invoke(cli, [tmp_path.joinpath("task.py").as_posix()])
596+
assert result.exit_code == ExitCode.OK
597+
assert "Collected 0 tasks" in result.output
598+
assert "Warning: The path" in result.output

0 commit comments

Comments
 (0)