Skip to content
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

Update choices setter for categorical widget to ensure _default_choices are updated when a callable choices is passed #563

Merged
merged 6 commits into from
Jul 2, 2023
Merged
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: 3 additions & 1 deletion src/magicgui/widgets/bases/_categorical_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def choices(self, choices: ChoicesType) -> None:
str_func = choices["key"]
elif not isinstance(choices, EnumMeta) and callable(choices):
_choices = choices(self)

# ensure that when setting choices with a callable,
# the default choices are also updated with that callable
self._default_choices = choices
else:
_choices = choices

Expand Down
17 changes: 17 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,23 @@ def test_categorical_change_choices(Cls):
assert wdg.choices == c


@pytest.mark.parametrize("Cls", [widgets.ComboBox, widgets.RadioButtons])
def test_categorical_change_choices_callable(Cls):
first_choices = ("a", "b")

def get_choices(wdg):
return ("c", "d")

wdg = Cls(choices=first_choices)
assert wdg.choices == first_choices
assert wdg._default_choices == first_choices

wdg.choices = get_choices

assert wdg.choices == ("c", "d")
assert wdg._default_choices == get_choices


@pytest.mark.skipif(use_app().backend_name != "qt", reason="only on qt")
def test_radiobutton_reset_choices():
"""Test that reset_choices doesn't change the number of buttons."""
Expand Down