Skip to content

Commit 2a09621

Browse files
committed
Reduce locations of warnings to 5.
1 parent 4c55e91 commit 2a09621

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/_pytask/warnings.py

Lines changed: 19 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def warn_now():
157157
assert "warning!!!" not in result.stdout.decode()
158158

159159

160-
def test_multiple_occurrences_of_warning(tmp_path, runner):
160+
def test_multiple_occurrences_of_warning_are_reduced(tmp_path, runner):
161161
source = """
162162
import warnings
163163
import pytask
@@ -175,4 +175,4 @@ def task_example():
175175
assert result.exit_code == ExitCode.OK
176176
assert "Warnings" in result.output
177177
assert "warning!!!" in result.output
178-
assert result.output.count("task_example") == 41
178+
assert result.output.count("task_example") == 31

0 commit comments

Comments
 (0)