From 407451ec30272fb6c33768de7799c09bd22160c1 Mon Sep 17 00:00:00 2001 From: SausageMan99 Date: Wed, 7 Oct 2026 18:15:13 +0200 Subject: [PATCH] fix(aggregation): Keep significant zeros in CAGrad string str(self.c).rstrip("0") also stripped significant zeros, so CAGrad(c=10) and CAGrad(c=100) were both named CAGrad1. Only the trailing ".0" of whole floats is now removed. Fixes #809 Co-Authored-By: Claude Opus 5.5 --- src/torchjd/aggregation/_cagrad.py | 2 +- tests/unit/aggregation/test_cagrad.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/torchjd/aggregation/_cagrad.py b/src/torchjd/aggregation/_cagrad.py index b2da3efe6..93b59c5a2 100644 --- a/src/torchjd/aggregation/_cagrad.py +++ b/src/torchjd/aggregation/_cagrad.py @@ -134,5 +134,5 @@ def __repr__(self) -> str: return f"{self.__class__.__name__}(c={self.c}, norm_eps={self.norm_eps})" def __str__(self) -> str: - c_str = str(self.c).rstrip("0") + c_str = str(self.c).removesuffix(".0") return f"CAGrad{c_str}" diff --git a/tests/unit/aggregation/test_cagrad.py b/tests/unit/aggregation/test_cagrad.py index 6cb5e9684..921cce13f 100644 --- a/tests/unit/aggregation/test_cagrad.py +++ b/tests/unit/aggregation/test_cagrad.py @@ -60,6 +60,22 @@ def test_representations() -> None: assert str(A) == "CAGrad0.5" +@mark.parametrize( + ["c", "expected"], + [ + (0, "CAGrad0"), + (0.0, "CAGrad0"), + (0.5, "CAGrad0.5"), + (1, "CAGrad1"), + (1.0, "CAGrad1"), + (10, "CAGrad10"), + (100.0, "CAGrad100"), + ], +) +def test_str_keeps_significant_zeros(c: float, expected: str) -> None: + assert str(CAGrad(c=c)) == expected + + def test_c_setter_updates_value() -> None: A = CAGrad(c=0.5) A.c = 1.25