Skip to content

add default values option for Dialog #1563

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

Closed
wants to merge 4 commits into from
Closed
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: 4 additions & 2 deletions prompt_toolkit/shortcuts/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def radiolist_dialog(
ok_text: str = "Ok",
cancel_text: str = "Cancel",
values: Optional[List[Tuple[_T, AnyFormattedText]]] = None,
default_values: List[_T] = [],
Copy link
Member

Choose a reason for hiding this comment

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

mutable values should not be used as keyword arguments. State could leak from one call to the next.

Better is to do:

default_values: Optional[List[_T]] = None,

and in the body of the function:

if default_values is None:
    default_values = []

style: Optional[BaseStyle] = None,
) -> Application[_T]:
"""
Expand All @@ -189,7 +190,7 @@ def radiolist_dialog(
def ok_handler() -> None:
get_app().exit(result=radio_list.current_value)

radio_list = RadioList(values)
radio_list = RadioList(values=values, default_values=default_values)

dialog = Dialog(
title=title,
Expand All @@ -213,6 +214,7 @@ def checkboxlist_dialog(
ok_text: str = "Ok",
cancel_text: str = "Cancel",
values: Optional[List[Tuple[_T, AnyFormattedText]]] = None,
default_values: List[_T] = [],
Copy link
Member

Choose a reason for hiding this comment

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

Same comment here about mutable defaults.

style: Optional[BaseStyle] = None,
) -> Application[List[_T]]:
"""
Expand All @@ -227,7 +229,7 @@ def checkboxlist_dialog(
def ok_handler() -> None:
get_app().exit(result=cb_list.current_values)

cb_list = CheckboxList(values)
cb_list = CheckboxList(values=values, default_values=default_values)

dialog = Dialog(
title=title,
Expand Down
20 changes: 16 additions & 4 deletions prompt_toolkit/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,14 +686,25 @@ class _DialogList(Generic[_T]):
multiple_selection: bool = False
show_scrollbar: bool = True

def __init__(self, values: Sequence[Tuple[_T, AnyFormattedText]]) -> None:
def __init__(
self, values: Sequence[Tuple[_T, AnyFormattedText]], default_values: List[_T]
Copy link
Member

Choose a reason for hiding this comment

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

Same comment about mutable defaults here.

) -> None:
assert len(values) > 0

self.values = values
# current_values will be used in multiple_selection,
# current_value will be used otherwise.
self.current_values: List[_T] = []
self.current_value: _T = values[0][0]
candidate: List[_T] = [value for (value, _) in values]
self.current_values: List[_T] = [
default_value
for default_value in default_values
if (default_value in candidate)
Copy link
Member

Choose a reason for hiding this comment

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

no parentheses are required around this expression.

]
self.current_value: _T = (
default_values[0]
if len(default_values) and default_values[0] in candidate
else values[0][0]
)
self._selected_index = 0

# Key bindings.
Expand Down Expand Up @@ -859,7 +870,8 @@ class Checkbox(CheckboxList[str]):

def __init__(self, text: AnyFormattedText = "", checked: bool = False) -> None:
values = [("value", text)]
CheckboxList.__init__(self, values)
default_values = ["value"]
CheckboxList.__init__(self, values=values, default_values=default_values)
self.checked = checked

@property
Expand Down