From 45130519e5e0f1c261f7c4eadebef972fdcadfa3 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Sat, 21 Dec 2024 07:23:27 -0800 Subject: [PATCH 1/3] testsuite: python: fix OutputFormat unit test Problem: A test in t/python/t0024-util.py doesn't specify the 'f' format type so OutputFormat will adjust width and precision as a string, which the test does not expect. Fix the test to use the 'f' presentation type. --- t/python/t0024-util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/python/t0024-util.py b/t/python/t0024-util.py index d8b6c6792f11..1050024ee0f5 100755 --- a/t/python/t0024-util.py +++ b/t/python/t0024-util.py @@ -207,9 +207,9 @@ def test_filter(self): self.assertEqual(fmt, "{i:>8} {f:8.2}") fmt = OutputFormat( - "?+:{i:>7} ?:{s:>6} ?+:{f:.2}", headings=self.headings + "?+:{i:>7} ?:{s:>6} ?+:{f:.2f}", headings=self.headings ).filter(items) - self.assertEqual(fmt, "{i:>8} {f:3.2}") + self.assertEqual(fmt, "{i:>8} {f:3.2f}") def test_sort(self): a = Item("a", 0, 2.2) From 564814a625071153bc2f6c0ef2f5dd9bc8a5fe8a Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Sat, 21 Dec 2024 07:28:15 -0800 Subject: [PATCH 2/3] python: fix precision OutputFormat expandable width Problem: The `+:` sentinel which expands width of fields doesn't adjust precision as well, which ends up truncating strings in a field that has been expanded to fit the largest value. The set of matching types needs to be expanded from ("s", None) to include the empty string to handle the case where a presentation type is not specified, so string is assumed. Add "" to the set of types for which to adjust precision. Fixes #6530 --- src/bindings/python/flux/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bindings/python/flux/util.py b/src/bindings/python/flux/util.py index 19374485f318..c2b360f1b370 100644 --- a/src/bindings/python/flux/util.py +++ b/src/bindings/python/flux/util.py @@ -741,7 +741,7 @@ def width(self, val): # Also adjust precision if necessary (only for string type) if ( - self.type in (None, "s") + self.type in (None, "s", "") and self.precision and self.precision < self.width ): From 27f4c50d0072c96aad1c59a85f8a31e151a8dee0 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Sat, 21 Dec 2024 07:37:55 -0800 Subject: [PATCH 3/3] testsuite: python: ensure OutputFormat precision handling Problem: No test in the testsuite ensures that the OutputFormat class adjusts precision for string presentation types if the width is adjusted due to the `+:` sentinel. Add a test that ensures correct handling of precision. --- t/python/t0024-util.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/t/python/t0024-util.py b/t/python/t0024-util.py index 1050024ee0f5..d16194393e1d 100755 --- a/t/python/t0024-util.py +++ b/t/python/t0024-util.py @@ -250,6 +250,17 @@ def test_sort(self): formatter.sort_items(items) self.assertListEqual(items, [z, d, a]) + def test_issue6530(self): + a = Item("1234567890", 0, 2.2) + b = Item("abcdefghijklmnop", 2, 13) + c = Item("c", 4, 5.0) + + items = [a, b, c] + fmt = OutputFormat( + "+:{s:5.5} +:{i:4d} +:{f:.2f}", headings=self.headings + ).filter(items) + self.assertEqual(fmt, "{s:16.16} {i:4d} {f:3.2f}") + if __name__ == "__main__": unittest.main(testRunner=TAPTestRunner())