Skip to content

Commit

Permalink
Drop support for mutable types as Dropdown options
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Jan 22, 2020
1 parent e643166 commit a498a2d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 64 deletions.
34 changes: 0 additions & 34 deletions ipywidgets/widgets/tests/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,40 +182,6 @@ def test_list_tuple_invalid():
print(bad) # because there is no custom message in assert_raises
c = interactive(f, tup=bad)

def test_dict():
for d in [
dict(a=5),
dict(a=5, b='b', c=dict),
]:
c = interactive(f, d=d)
w = c.children[0]
check = dict(
cls=widgets.Dropdown,
description='d',
value=next(iter(d.values())),
options=d,
_options_labels=tuple(d.keys()),
_options_values=tuple(d.values()),
)
check_widget(w, **check)


def test_ordereddict():
from collections import OrderedDict
items = [(3, 300), (1, 100), (2, 200)]
first = items[0][1]
values = OrderedDict(items)
c = interactive(f, lis=values)
assert len(c.children) == 2
d = dict(
cls=widgets.Dropdown,
value=first,
options=values,
_options_labels=("3", "1", "2"),
_options_values=(300, 100, 200),
)
check_widgets(c, lis=d)

def test_iterable():
def yield_values():
yield 3
Expand Down
16 changes: 0 additions & 16 deletions ipywidgets/widgets/tests/test_widget_selection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import inspect
import warnings
from unittest import TestCase

from traitlets import TraitError
Expand All @@ -15,20 +13,6 @@ class TestDropdown(TestCase):
def test_construction(self):
Dropdown()

def test_deprecation_warning_mapping_options(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")

# Clearing the internal __warningregistry__ seems to be required for
# Python 2 (but not for Python 3)
module = inspect.getmodule(Dropdown)
getattr(module, '__warningregistry__', {}).clear()

Dropdown(options={'One': 1, 'Two': 2, 'Three': 3})
assert len(w) > 0
assert issubclass(w[-1].category, DeprecationWarning)
assert "Support for mapping types has been deprecated" in str(w[-1].message)


class TestSelectionSlider(TestCase):

Expand Down
20 changes: 6 additions & 14 deletions ipywidgets/widgets/widget_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ def _make_options(x):
* an iterable of (label, value) pairs
* an iterable of values, and labels will be generated
"""
# Check if x is a mapping of labels to values
if isinstance(x, Mapping):
import warnings
warnings.warn("Support for mapping types has been deprecated and will be dropped in a future release.", DeprecationWarning)
return tuple((str(k), v) for k, v in x.items())

# only iterate once through the options.
xlist = tuple(x)

Expand All @@ -132,10 +126,10 @@ def findvalue(array, value, compare = lambda x, y: x == y):
class _Selection(DescriptionWidget, ValueWidget, CoreWidget):
"""Base class for Selection widgets
``options`` can be specified as a list of values, list of (label, value)
tuples, or a dict of {label: value}. The labels are the strings that will be
displayed in the UI, representing the actual Python choices, and should be
unique. If labels are not specified, they are generated from the values.
``options`` can be specified as a list of values or list of (label, value)
tuples. The labels are the strings that will be displayed in the UI,
representing the actual Python choices, and should be unique.
If labels are not specified, they are generated from the values.
When programmatically setting the value, a reverse lookup is performed
among the options to check that the value is valid. The reverse lookup uses
Expand All @@ -149,7 +143,7 @@ class _Selection(DescriptionWidget, ValueWidget, CoreWidget):
index = Int(None, help="Selected index", allow_none=True).tag(sync=True)

options = Any((),
help="""Iterable of values, (label, value) pairs, or a mapping of {label: value} pairs that the user can select.
help="""Iterable of values or (label, value) pairs that the user can select.
The labels are the strings that will be displayed in the UI, representing the
actual Python choices, and should be unique.
Expand Down Expand Up @@ -183,9 +177,7 @@ def __init__(self, *args, **kwargs):

@validate('options')
def _validate_options(self, proposal):
# if an iterator is provided, exhaust it
if isinstance(proposal.value, Iterable) and not isinstance(proposal.value, Mapping):
proposal.value = tuple(proposal.value)
proposal.value = tuple(proposal.value)
# throws an error if there is a problem converting to full form
self._options_full = _make_options(proposal.value)
return proposal.value
Expand Down

0 comments on commit a498a2d

Please sign in to comment.