diff --git a/questionary/prompts/common.py b/questionary/prompts/common.py index d07d0e63..c2819ff5 100644 --- a/questionary/prompts/common.py +++ b/questionary/prompts/common.py @@ -104,7 +104,9 @@ def build(c: Union[str, "Choice", Dict[str, Any]]) -> "Choice": """Create a choice object from different representations. Args: - c: Either a :obj:`str`, :class:`Choice` or :obj:`dict` with + c: Either a :obj:`str` (or an :obj:`int` / :obj:`bool` / + :obj:`float` converted + to :obj:`str`), :class:`Choice` or :obj:`dict` with ``name``, ``value``, ``disabled``, ``checked`` and ``key`` properties. @@ -116,6 +118,8 @@ def build(c: Union[str, "Choice", Dict[str, Any]]) -> "Choice": return c elif isinstance(c, str): return Choice(c, c) + elif isinstance(c, (int, bool, float)): + return Choice(str(c), c) else: return Choice( c.get("name"), diff --git a/tests/prompts/test_common.py b/tests/prompts/test_common.py index 13a93e2e..74a0ddb2 100644 --- a/tests/prompts/test_common.py +++ b/tests/prompts/test_common.py @@ -271,3 +271,38 @@ def test_prompt_show_description(): ] assert ic.pointed_at == 1 assert ic._get_choice_tokens() == expected_tokens + + +def test_non_string_choices_handling(): + """Test selecting a choice from a list containing non-string items.""" + ic = InquirerControl([1, 2.5, True, "String"]) + + expected_tokens = [ + ("class:pointer", " » "), + ("[SetCursorPosition]", ""), + ("class:text", "○ "), + ("class:highlighted", "1"), + ("", "\n"), + ("class:text", " "), + ("class:text", "○ "), + ("class:text", "2.5"), + ("", "\n"), + ("class:text", " "), + ("class:text", "○ "), + ("class:text", "True"), + ("", "\n"), + ("class:text", " "), + ("class:text", "○ "), + ("class:text", "String"), + ] + assert ic.pointed_at == 0 + assert ic._get_choice_tokens() == expected_tokens + + ic.select_next() + assert ic.get_pointed_at().value == 2.5 + + ic.select_next() + assert ic.get_pointed_at().value is True + + ic.select_next() + assert ic.get_pointed_at().value == "String"