Skip to content

Commit d848427

Browse files
Merge pull request #2130 from jtpio/mapping-deprecation-warning
Add DeprecationWarning when using Mapping for options
2 parents 2ce067a + c1452f3 commit d848427

File tree

4 files changed

+34
-50
lines changed

4 files changed

+34
-50
lines changed

docs/source/examples/Using Interact.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@
499499
"cell_type": "markdown",
500500
"metadata": {},
501501
"source": [
502-
"If you want a dropdown menu that passes non-string values to the Python function, you can pass a dictionary. The keys in the dictionary are used for the names in the dropdown menu UI and the values are the arguments that are passed to the underlying Python function."
502+
"If you want a dropdown menu that passes non-string values to the Python function, you can pass a list of (label, value) pairs."
503503
]
504504
},
505505
{
@@ -515,15 +515,15 @@
515515
"version_minor": 0
516516
},
517517
"text/plain": [
518-
"interactive(children=(Dropdown(description='x', options={'one': 10, 'two': 20}, value=10), Output()), _dom_cla"
518+
"interactive(children=(Dropdown(description='x', options=(('one', 10), ('two', 20)), value=10), Output()), _dom"
519519
]
520520
},
521521
"metadata": {},
522522
"output_type": "display_data"
523523
}
524524
],
525525
"source": [
526-
"interact(f, x={'one': 10, 'two': 20});"
526+
"interact(f, x=[('one', 10), ('two', 20)]);"
527527
]
528528
},
529529
{

docs/source/examples/Widget List.ipynb

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@
686686
"cell_type": "markdown",
687687
"metadata": {},
688688
"source": [
689-
"There are several widgets that can be used to display single selection lists, and two that can be used to select multiple values. All inherit from the same base class. You can specify the **enumeration of selectable options by passing a list** (options are either (label, value) pairs, or simply values for which the labels are derived by calling `str`). You can **also specify the enumeration as a dictionary**, in which case the **keys will be used as the item displayed** in the list and the corresponding **value will be used** when an item is selected (in this case, since dictionaries are unordered, the displayed order of items in the widget is unspecified)."
689+
"There are several widgets that can be used to display single selection lists, and two that can be used to select multiple values. All inherit from the same base class. You can specify the **enumeration of selectable options by passing a list** (options are either (label, value) pairs, or simply values for which the labels are derived by calling `str`)."
690690
]
691691
},
692692
{
@@ -729,41 +729,6 @@
729729
")"
730730
]
731731
},
732-
{
733-
"cell_type": "markdown",
734-
"metadata": {},
735-
"source": [
736-
"The following is also valid:"
737-
]
738-
},
739-
{
740-
"cell_type": "code",
741-
"execution_count": 18,
742-
"metadata": {},
743-
"outputs": [
744-
{
745-
"data": {
746-
"application/vnd.jupyter.widget-view+json": {
747-
"model_id": "91ed9917c1f84bd890c071bf40ffbbf8",
748-
"version_major": 2,
749-
"version_minor": 0
750-
},
751-
"text/plain": [
752-
"Dropdown(description='Number:', index=1, options={'One': 1, 'Two': 2, 'Three': 3}, value=2)"
753-
]
754-
},
755-
"metadata": {},
756-
"output_type": "display_data"
757-
}
758-
],
759-
"source": [
760-
"widgets.Dropdown(\n",
761-
" options={'One': 1, 'Two': 2, 'Three': 3},\n",
762-
" value=2,\n",
763-
" description='Number:',\n",
764-
")"
765-
]
766-
},
767732
{
768733
"cell_type": "markdown",
769734
"metadata": {
@@ -3411,5 +3376,5 @@
34113376
}
34123377
},
34133378
"nbformat": 4,
3414-
"nbformat_minor": 1
3379+
"nbformat_minor": 2
34153380
}

ipywidgets/widgets/tests/test_widget_selection.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
import inspect
5+
import warnings
46
from unittest import TestCase
57

68
from traitlets import TraitError
79

8-
from ipywidgets import SelectionSlider, Select
10+
from ipywidgets import Dropdown, SelectionSlider, Select
11+
12+
13+
class TestDropdown(TestCase):
14+
15+
def test_construction(self):
16+
Dropdown()
17+
18+
def test_deprecation_warning_mapping_options(self):
19+
with warnings.catch_warnings(record=True) as w:
20+
warnings.simplefilter("always")
21+
22+
# Clearing the internal __warningregistry__ seems to be required for
23+
# Python 2 (but not for Python 3)
24+
module = inspect.getmodule(Dropdown)
25+
getattr(module, '__warningregistry__', {}).clear()
26+
27+
Dropdown(options={'One': 1, 'Two': 2, 'Three': 3})
28+
assert len(w) > 0
29+
assert issubclass(w[-1].category, DeprecationWarning)
30+
assert "Support for mapping types has been deprecated" in str(w[-1].message)
31+
932

1033
class TestSelectionSlider(TestCase):
1134

ipywidgets/widgets/widget_selection.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,11 @@
2626

2727
_doc_snippets = {}
2828
_doc_snippets['selection_params'] = """
29-
options: list or dict
29+
options: list
3030
The options for the dropdown. This can either be a list of values, e.g.
31-
``['Galileo', 'Brahe', 'Hubble']`` or ``[0, 1, 2]``, a list of
31+
``['Galileo', 'Brahe', 'Hubble']`` or ``[0, 1, 2]``, or a list of
3232
(label, value) pairs, e.g.
33-
``[('Galileo', 0), ('Brahe', 1), ('Hubble', 2)]``,
34-
or a dictionary mapping the labels to the values, e.g. ``{'Galileo': 0,
35-
'Brahe': 1, 'Hubble': 2}``. The labels are the strings that will be
36-
displayed in the UI, representing the actual Python choices, and should
37-
be unique. If this is a dictionary, the order in which they are
38-
displayed is not guaranteed.
33+
``[('Galileo', 0), ('Brahe', 1), ('Hubble', 2)]``.
3934
4035
index: int
4136
The index of the current selection.
@@ -113,12 +108,13 @@ def _make_options(x):
113108
The returned tuple should be in the format (('label', value), ('label', value), ...).
114109
115110
The input can be
116-
* a Mapping of labels to values
117111
* an iterable of (label, value) pairs
118112
* an iterable of values, and labels will be generated
119113
"""
120114
# Check if x is a mapping of labels to values
121115
if isinstance(x, Mapping):
116+
import warnings
117+
warnings.warn("Support for mapping types has been deprecated and will be dropped in a future release.", DeprecationWarning)
122118
return tuple((unicode_type(k), v) for k, v in x.items())
123119

124120
# only iterate once through the options.

0 commit comments

Comments
 (0)