From 46b410c60dca17e77ff76a87900955b4ef20f120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryd=C3=A9n=20Johan?= Date: Fri, 17 Jul 2026 14:15:59 +0200 Subject: [PATCH] timer: ignore cancelled timers without samples --- tests/test_timer.py | 13 +++++++++++++ toolkit/timer.py | 2 ++ 2 files changed, 15 insertions(+) create mode 100644 tests/test_timer.py diff --git a/tests/test_timer.py b/tests/test_timer.py new file mode 100644 index 00000000..7f4945ec --- /dev/null +++ b/tests/test_timer.py @@ -0,0 +1,13 @@ +from toolkit.timer import Timer + + +def test_print_ignores_cancelled_timer_without_samples(): + timer = Timer() + printed_timings = [] + timer.add_after_print_hook(printed_timings.append) + + timer.start("cancelled") + timer.cancel("cancelled") + timer.print() + + assert printed_timings == [{}] diff --git a/toolkit/timer.py b/toolkit/timer.py index e849ba5f..09779dc8 100644 --- a/toolkit/timer.py +++ b/toolkit/timer.py @@ -48,6 +48,8 @@ class Timer: timing_dict = {} # sort by longest at top for timer_name, timings in sorted(self.timers.items(), key=lambda x: sum(x[1]), reverse=True): + if not timings: + continue avg_time = sum(timings) / len(timings) if not is_ui: