Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion questionary/prompts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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"),
Expand Down
35 changes: 35 additions & 0 deletions tests/prompts/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading