Skip to content

Commit

Permalink
Variable: Check number of values in DiscreteVariable.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Feb 7, 2020
1 parent bee3df1 commit 66961cc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
17 changes: 17 additions & 0 deletions Orange/data/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ def varcls_modified(self, name):
var.ordered = True
return var

def test_copy_checks_len_values(self):
var = DiscreteVariable("gender", values=["F", "M"])
self.assertEqual(var.values, ["F", "M"])

self.assertRaises(ValueError, var.copy, values=["F", "M", "N"])
self.assertRaises(ValueError, var.copy, values=["F"])
self.assertRaises(ValueError, var.copy, values=[])

var2 = var.copy()
self.assertEqual(var2.values, ["F", "M"])

var2 = var.copy(values=None)
self.assertEqual(var2.values, ["F", "M"])

var2 = var.copy(values=["W", "M"])
self.assertEqual(var2.values, ["W", "M"])


@variabletest(ContinuousVariable)
class TestContinuousVariable(VariableTest):
Expand Down
3 changes: 3 additions & 0 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,9 @@ def __reduce__(self):
__dict__

def copy(self, compute_value=None, *, name=None, values=None, **_):
if values is not None and len(values) != len(self.values):
raise ValueError(
"number of values must match the number of original values")
return super().copy(compute_value=compute_value, name=name,
values=values or self.values, ordered=self.ordered)

Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/utils/colorbrewer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings

warnings.warn("Module 'colorbrewer' is obsolete and will be removed.\n"
"Use palettes from 'Orange.misccolorpalettes'.",
"Use palettes from 'Orange.widget.utils.colorpalettes'.",
DeprecationWarning)

colorSchemes = {
Expand Down
13 changes: 4 additions & 9 deletions Orange/widgets/utils/colorpalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
from Orange.widgets import gui
from Orange.widgets.utils import colorbrewer

warnings.warn(
"Module colorpalette is obsolete; use colorpalettes", DeprecationWarning)


DefaultRGBColors = [
(70, 190, 250), (237, 70, 47), (170, 242, 43), (245, 174, 50), (255, 255, 0),
(255, 0, 255), (0, 255, 255), (128, 0, 255), (0, 128, 255), (255, 223, 128),
Expand Down Expand Up @@ -392,12 +396,6 @@ def colorSchemaChange(self):
class ColorPalleteListing(QDialog):
def __init__(self, parent=None, windowTitle="Color Palette List",
**kwargs):
# Import locally to prevent warning about using an obsolete module
# (This module itself is obsolete, but imported by its replacement,
# for backward compatibility, so importing colorbrewer globally would
# cause a warning.)
from Orange.widgets.utils import colorbrewer

super().__init__(parent, windowTitle=windowTitle, **kwargs)
self.setLayout(QVBoxLayout())
self.layout().setContentsMargins(0, 0, 0, 0)
Expand Down Expand Up @@ -1016,8 +1014,5 @@ def main(): # pragma: no cover
a.exec()


warnings.warn(
"Module colorpalette is obsolete; use colorpalettes", DeprecationWarning)

if __name__ == "__main__": # pragma: no cover
main()
2 changes: 1 addition & 1 deletion Orange/widgets/visualize/utils/owlegend.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def set_domain(self, domain):
def set_items(self, values):
vals, palette = values
if self.orientation == Qt.Vertical:
vals = reversed(vals)
vals = list(reversed(vals))
self._layout.addItem(ContinuousLegendItem(
palette=palette,
values=vals,
Expand Down

0 comments on commit 66961cc

Please sign in to comment.