Skip to content

Commit a8d9e8a

Browse files
authored
Reduce number of locations where warnings are raised. (#278)
1 parent bec6d2e commit a8d9e8a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

docs/source/changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
1111
- {pull}`277` ignores `DeprecationWarning` and `PendingDeprecationWarning` by default.
1212
Previously, they were enabled, but they should be shown when testing the project with
1313
pytest, not after the execution with pytask. Fixes {issue}`269`.
14+
- {pull}`278` counts multiple occurrences of a warning instead of listing the module or
15+
task name again and again. Fixes {issue}`270`.
1416

1517
## 0.2.2 - 2022-05-14
1618

src/_pytask/warnings.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ def pytask_log_session_footer(session: Session) -> None:
249249
grouped_warnings[warning.message].append(location)
250250
sorted_gw = {k: sorted(v) for k, v in grouped_warnings.items()}
251251

252-
renderable = MyRenderable(sorted_gw)
252+
reduced_gw = _reduce_grouped_warnings(sorted_gw)
253+
254+
renderable = MyRenderable(reduced_gw)
253255

254256
panel = Panel(renderable, title="Warnings", style="warning")
255257
console.print(panel)
@@ -271,3 +273,19 @@ def __rich_console__(
271273
"[bold red]♥[/bold red] "
272274
+ "https://pytask-dev.rtdf.io/en/stable/how_to_guides/capture_warnings.html"
273275
)
276+
277+
278+
def _reduce_grouped_warnings(
279+
grouped_warnings: dict[str, list[str]], max_locations: int = 5
280+
) -> dict[str, list[str]]:
281+
"""Reduce grouped warnings."""
282+
reduced_gw = {}
283+
for message, locations in grouped_warnings.items():
284+
if len(locations) > max_locations:
285+
adjusted_locations = locations[:max_locations]
286+
n_more_locations = len(locations[max_locations:])
287+
adjusted_locations.append(f"... in {n_more_locations} more locations.")
288+
else:
289+
adjusted_locations = locations
290+
reduced_gw[message] = adjusted_locations
291+
return reduced_gw

tests/test_warnings.py

+21
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,24 @@ def warn_now():
155155
assert result.returncode == ExitCode.OK
156156
assert "Warnings" not in result.stdout.decode()
157157
assert "warning!!!" not in result.stdout.decode()
158+
159+
160+
def test_multiple_occurrences_of_warning_are_reduced(tmp_path, runner):
161+
source = """
162+
import warnings
163+
import pytask
164+
165+
for i in range(10):
166+
167+
@pytask.mark.task
168+
def task_example():
169+
warnings.warn("warning!!!")
170+
"""
171+
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
172+
173+
result = runner.invoke(cli, [tmp_path.as_posix()])
174+
175+
assert result.exit_code == ExitCode.OK
176+
assert "Warnings" in result.output
177+
assert "warning!!!" in result.output
178+
assert result.output.count("task_example") == 31

0 commit comments

Comments
 (0)