From 49601bb4fcd87340b2212038edb8db4807ed30e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sun, 21 Jun 2026 19:48:34 +0200 Subject: [PATCH] feat(utils): add label_with_details helper Builds a "Name (detail, detail)" label from a name plus optional details, dropping falsy parts and omitting the parentheses entirely when none remain. Extracted to deduplicate the "filter present parts, join, wrap in parens" idiom that several model display properties share. Co-Authored-By: Claude Opus 4.8 (1M context) --- common/utils.py | 11 +++++++++++ tests/test_utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/test_utils.py diff --git a/common/utils.py b/common/utils.py index ec74aca..2c376b3 100644 --- a/common/utils.py +++ b/common/utils.py @@ -114,6 +114,17 @@ def format_float_or_int(number: int | float): return int(number) if float(number).is_integer() else f"{number:03.2f}" +def label_with_details(name: str, *details: object, separator: str = ", ") -> str: + """Build a ``"Name (detail, detail)"`` label from a name and optional details. + + Falsy details (``None``, ``""``, ``0``) are dropped; the rest are stringified + and joined with ``separator`` inside parentheses. With no details remaining, + the bare ``name`` is returned without parentheses. + """ + present = [str(detail) for detail in details if detail] + return f"{name} ({separator.join(present)})" if present else name + + OperatorType = Literal["|", "&"] diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..7b3823a --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,25 @@ +import unittest + +from common.utils import label_with_details + + +class LabelWithDetailsTest(unittest.TestCase): + def test_all_parts_present(self): + self.assertEqual( + label_with_details("Mario", "Steam", 2020), "Mario (Steam, 2020)" + ) + + def test_some_parts_falsy(self): + self.assertEqual(label_with_details("Mario", None, 2020), "Mario (2020)") + self.assertEqual(label_with_details("Mario", "Steam", None), "Mario (Steam)") + + def test_all_parts_falsy(self): + self.assertEqual(label_with_details("Mario", None, "", 0), "Mario") + + def test_no_details(self): + self.assertEqual(label_with_details("Mario"), "Mario") + + def test_custom_separator(self): + self.assertEqual( + label_with_details("Mario", "a", "b", separator=" / "), "Mario (a / b)" + )