From 4c55e913c03deeedd8535ba479474f9f4284688b Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Sun, 22 May 2022 23:50:58 +0200 Subject: [PATCH 1/2] Add test for a warning in the same location raised by parametrized tasks. --- docs/source/changes.md | 2 ++ tests/test_warnings.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/docs/source/changes.md b/docs/source/changes.md index 7fc4f13f..5f30e3a3 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -11,6 +11,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and - {pull}`277` ignores `DeprecationWarning` and `PendingDeprecationWarning` by default. Previously, they were enabled, but they should be shown when testing the project with pytest, not after the execution with pytask. Fixes {issue}`269`. +- {pull}`278` counts multiple occurrences of a warning instead of listing the module or + task name again and again. Fixes {issue}`270`. ## 0.2.2 - 2022-05-14 diff --git a/tests/test_warnings.py b/tests/test_warnings.py index a07410c4..f1572285 100644 --- a/tests/test_warnings.py +++ b/tests/test_warnings.py @@ -155,3 +155,24 @@ def warn_now(): assert result.returncode == ExitCode.OK assert "Warnings" not in result.stdout.decode() assert "warning!!!" not in result.stdout.decode() + + +def test_multiple_occurrences_of_warning(tmp_path, runner): + source = """ + import warnings + import pytask + + for i in range(10): + + @pytask.mark.task + def task_example(): + warnings.warn("warning!!!") + """ + tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source)) + + result = runner.invoke(cli, [tmp_path.as_posix()]) + + assert result.exit_code == ExitCode.OK + assert "Warnings" in result.output + assert "warning!!!" in result.output + assert result.output.count("task_example") == 41 From 2a096216bb3abf8f12d5a0da3517cfe6d55ac815 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Mon, 23 May 2022 00:05:32 +0200 Subject: [PATCH 2/2] Reduce locations of warnings to 5. --- src/_pytask/warnings.py | 20 +++++++++++++++++++- tests/test_warnings.py | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/_pytask/warnings.py b/src/_pytask/warnings.py index 12a1cba4..ef5d3002 100644 --- a/src/_pytask/warnings.py +++ b/src/_pytask/warnings.py @@ -249,7 +249,9 @@ def pytask_log_session_footer(session: Session) -> None: grouped_warnings[warning.message].append(location) sorted_gw = {k: sorted(v) for k, v in grouped_warnings.items()} - renderable = MyRenderable(sorted_gw) + reduced_gw = _reduce_grouped_warnings(sorted_gw) + + renderable = MyRenderable(reduced_gw) panel = Panel(renderable, title="Warnings", style="warning") console.print(panel) @@ -271,3 +273,19 @@ def __rich_console__( "[bold red]♥[/bold red] " + "https://pytask-dev.rtdf.io/en/stable/how_to_guides/capture_warnings.html" ) + + +def _reduce_grouped_warnings( + grouped_warnings: dict[str, list[str]], max_locations: int = 5 +) -> dict[str, list[str]]: + """Reduce grouped warnings.""" + reduced_gw = {} + for message, locations in grouped_warnings.items(): + if len(locations) > max_locations: + adjusted_locations = locations[:max_locations] + n_more_locations = len(locations[max_locations:]) + adjusted_locations.append(f"... in {n_more_locations} more locations.") + else: + adjusted_locations = locations + reduced_gw[message] = adjusted_locations + return reduced_gw diff --git a/tests/test_warnings.py b/tests/test_warnings.py index f1572285..12e50961 100644 --- a/tests/test_warnings.py +++ b/tests/test_warnings.py @@ -157,7 +157,7 @@ def warn_now(): assert "warning!!!" not in result.stdout.decode() -def test_multiple_occurrences_of_warning(tmp_path, runner): +def test_multiple_occurrences_of_warning_are_reduced(tmp_path, runner): source = """ import warnings import pytask @@ -175,4 +175,4 @@ def task_example(): assert result.exit_code == ExitCode.OK assert "Warnings" in result.output assert "warning!!!" in result.output - assert result.output.count("task_example") == 41 + assert result.output.count("task_example") == 31