From 8fc52dc6c207677632ff8f3f20407514e603a707 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Fri, 12 Jul 2024 20:07:25 +0200 Subject: [PATCH] Add warning when non-matching files are passed to pytask. --- docs/source/changes.md | 3 +++ src/_pytask/config.py | 14 ++++++++++++++ tests/test_collect.py | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/docs/source/changes.md b/docs/source/changes.md index d4ce3a5d..893eef59 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -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 diff --git a/src/_pytask/config.py b/src/_pytask/config.py index 86423ac3..274178e8 100644 --- a/src/_pytask/config.py +++ b/src/_pytask/config.py @@ -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 @@ -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") diff --git a/tests/test_collect.py b/tests/test_collect.py index 93591998..b8ec63a6 100644 --- a/tests/test_collect.py +++ b/tests/test_collect.py @@ -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