From e5b6949114eeb91a0b9c9755a4d5589ce168787b Mon Sep 17 00:00:00 2001 From: janezd Date: Fri, 7 Feb 2020 12:11:42 +0100 Subject: [PATCH] Color palettes: Fixes related to review --- Orange/data/tests/test_variable.py | 17 +++++++++++++++++ Orange/data/variable.py | 3 +++ Orange/widgets/utils/colorbrewer.py | 2 +- Orange/widgets/utils/colorpalette.py | 13 ++++--------- Orange/widgets/visualize/utils/owlegend.py | 2 +- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Orange/data/tests/test_variable.py b/Orange/data/tests/test_variable.py index 286268f83e5..bb6f92533fe 100644 --- a/Orange/data/tests/test_variable.py +++ b/Orange/data/tests/test_variable.py @@ -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): diff --git a/Orange/data/variable.py b/Orange/data/variable.py index 8e59592d938..b12ae54c7ed 100644 --- a/Orange/data/variable.py +++ b/Orange/data/variable.py @@ -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) diff --git a/Orange/widgets/utils/colorbrewer.py b/Orange/widgets/utils/colorbrewer.py index 46d6cd6008c..30267353f89 100644 --- a/Orange/widgets/utils/colorbrewer.py +++ b/Orange/widgets/utils/colorbrewer.py @@ -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 = { diff --git a/Orange/widgets/utils/colorpalette.py b/Orange/widgets/utils/colorpalette.py index 590f62efbc3..e52c6a7167c 100644 --- a/Orange/widgets/utils/colorpalette.py +++ b/Orange/widgets/utils/colorpalette.py @@ -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), @@ -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) @@ -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() diff --git a/Orange/widgets/visualize/utils/owlegend.py b/Orange/widgets/visualize/utils/owlegend.py index 96ad391325e..0e27c2e410b 100644 --- a/Orange/widgets/visualize/utils/owlegend.py +++ b/Orange/widgets/visualize/utils/owlegend.py @@ -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,