Skip to content

Added default parameter to RadioList and radiolist_dialog #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions prompt_toolkit/shortcuts/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ def message_dialog(title='', text='', ok_text='Ok', style=None, async_=False):


def radiolist_dialog(title='', text='', ok_text='Ok', cancel_text='Cancel',
values=None, style=None, async_=False):
values=None, style=None, async_=False, default=0):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the default argument, but I would prefer if it was not an integer, but a key from the values list. Would it be possible to change it that way. The default can be None which always takes the first value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd hesitate because the input is a list of tuples. There is no guarantee of uniqueness on the values or key. Maybe also accept the tuple from the list? And do a check internally and decide what to do?

if default is None:
  self.current_value = 0
elif isinstance(default, tuple):
  self.current_value = values.index(default)
else:
  self.current_value = default

"""
Display a simple message box and wait until the user presses enter.
"""
def ok_handler():
get_app().exit(result=radio_list.current_value)

radio_list = RadioList(values)
radio_list = RadioList(values, default)

dialog = Dialog(
title=title,
Expand Down
10 changes: 7 additions & 3 deletions prompt_toolkit/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,16 +497,20 @@ class RadioList(object):
List of radio buttons. Only one can be checked at the same time.

:param values: List of (value, label) tuples.
:param default: Default index to select, defaults to 0
"""
def __init__(self, values):
def __init__(self, values, default=0):
assert isinstance(values, list)
assert len(values) > 0
assert all(isinstance(i, tuple) and len(i) == 2
for i in values)
assert isinstance(default, int)
assert 0 <= default < len(values)

self.values = values
self.current_value = values[0][0]
self._selected_index = 0

self.current_value = values[default][0]
self._selected_index = default

# Key bindings.
kb = KeyBindings()
Expand Down
96 changes: 96 additions & 0 deletions tests/test_radiolist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from __future__ import unicode_literals

from prompt_toolkit.widgets import RadioList

import pytest

def test_initial():
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values)
assert radiolist.current_value == 1
assert radiolist._selected_index == 0

def test_default():
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values, 1)
assert radiolist.current_value == 'fizz'
assert radiolist._selected_index == 1

def test_bad_params():
with pytest.raises(AssertionError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Arrowbox,
Thanks a lot for writing unit tests, but for these, we shouldn't ever assume that AssertionError is ever raised. If Python runs with the -O option, then no assertions are ever raised. Removing an assertion from the code is not supposed to break something, so there can't be a unit test which states it does.

I think raising ValueError is more appropriate, if we want to have some flow control in case of passing a bad input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to change that, but there are a bunch of other assert precondition statements at the beginning of the function already. I was trying to follow the previous format and thought an extra test there would be okay. Should I change all the checks? Or maybe just drop the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at testing if assert has been stripped to decide if we should just drop the test but that did not seem trivial.

radiolist = RadioList([])

with pytest.raises(AssertionError):
radiolist = RadioList(None)

with pytest.raises(AssertionError):
values = (
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
)
radiolist = RadioList(values)

with pytest.raises(AssertionError):
values = [
(1, 'some_text', 'whoops'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values)

with pytest.raises(AssertionError):
values = [
(1, 'some_text'),
('fizz', ),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values)

with pytest.raises(AssertionError):
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
[{'foo':'bar'}, 'even_more_text']
]
radiolist = RadioList(values)

with pytest.raises(AssertionError):
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values, 3)

with pytest.raises(AssertionError):
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values, -1)

with pytest.raises(AssertionError):
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values, None)

with pytest.raises(AssertionError):
values = [
(1, 'some_text'),
('fizz', 'some_more_text'),
({'foo':'bar'}, 'even_more_text')
]
radiolist = RadioList(values, 'whoops')