From fd2ba3a2b3a7480990937a458edee2f053009e90 Mon Sep 17 00:00:00 2001 From: janezd Date: Fri, 6 Dec 2019 14:56:33 +0100 Subject: [PATCH] Select Rows: Add code for support of old settings and ensure its removal in the future --- Orange/widgets/data/owselectrows.py | 24 +++++++--- .../widgets/data/tests/test_owselectrows.py | 44 ++++++++++++++++--- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Orange/widgets/data/owselectrows.py b/Orange/widgets/data/owselectrows.py index 309473129b9..1b95552639b 100644 --- a/Orange/widgets/data/owselectrows.py +++ b/Orange/widgets/data/owselectrows.py @@ -56,7 +56,12 @@ def encode_setting(self, context, setting, value): def decode_setting(self, setting, value, domain=None): value = super().decode_setting(setting, value, domain) if setting.name == 'conditions': - for i, (attr, _, op, values) in enumerate(value): + # Use this after 2021/1/1: + # for i, (attr, _, op, values) in enumerate(value): + for i, condition in enumerate(value): + attr = condition[0] + op, values = condition[-2:] + var = attr in domain and domain[attr] if var and var.is_continuous and not isinstance(var, TimeVariable): value[i] = (attr, op, @@ -71,7 +76,10 @@ def match(self, context, domain, attrs, metas): conditions = context.values["conditions"] all_vars = attrs all_vars.update(metas) - if all(all_vars.get(name) == tpe for name, tpe, *_ in conditions): + # Use this after 2021/1/1: + # if all(all_vars.get(name) == tpe for name, tpe, *_ in conditions): + if all(all_vars.get(name) == tpe if len(rest) == 2 else name in all_vars + for name, tpe, *rest in conditions): return 0.5 return self.NO_MATCH @@ -705,11 +713,13 @@ def send_report(self): ("Non-matching data", nonmatch_inst > 0 and "{} instances".format(nonmatch_inst)))) - @classmethod - def migrate_context(cls, context, version): - if not version: - # Just remove; can't migrate because variables types are unknown - context.values["conditions"] = [] + # Uncomment this on 2021/1/1 + # + # @classmethod + # def migrate_context(cls, context, version): + # if not version: + # # Just remove; can't migrate because variables types are unknown + # context.values["conditions"] = [] class CheckBoxPopup(QWidget): diff --git a/Orange/widgets/data/tests/test_owselectrows.py b/Orange/widgets/data/tests/test_owselectrows.py index 255315a74e9..426fc6711ee 100644 --- a/Orange/widgets/data/tests/test_owselectrows.py +++ b/Orange/widgets/data/tests/test_owselectrows.py @@ -1,5 +1,7 @@ # Test methods with long descriptive names can omit docstrings # pylint: disable=missing-docstring +import time + from AnyQt.QtCore import QLocale, Qt from AnyQt.QtTest import QTest from AnyQt.QtWidgets import QLineEdit, QComboBox @@ -259,15 +261,43 @@ def test_change_var_type(self): new_iris = iris.transform(new_domain) self.send_signal(self.widget.Inputs.data, new_iris) - def test_migration_to_version_1(self): + # Uncomment this on 2021/1/1 + # + # def test_migration_to_version_1(self): + # iris = Table("iris") + # + # ch = SelectRowsContextHandler() + # context = ch.new_context(iris.domain, *ch.encode_domain(iris.domain)) + # context.values = dict(conditions=[["petal length", 2, (5.2,)]]) + # settings = dict(context_settings=[context]) + # widget = self.create_widget(OWSelectRows, settings) + # self.assertEqual(widget.conditions, []) + + @override_locale(QLocale.C) + def test_support_old_settings(self): iris = Table("iris") + self.widget = self.widget_with_context( + iris.domain, [["sepal length", 2, ("5.2",)]]) + self.send_signal(self.widget.Inputs.data, iris) + condition = self.widget.conditions[0] + self.assertEqual(condition[0], "sepal length") + self.assertEqual(condition[1], 2) + self.assertTrue(condition[2][0].startswith("5.2")) - ch = SelectRowsContextHandler() - context = ch.new_context(iris.domain, *ch.encode_domain(iris.domain)) - context.values = dict(conditions=[["petal length", 2, (5.2,)]]) - settings = dict(context_settings=[context]) - widget = self.create_widget(OWSelectRows, settings) - self.assertEqual(widget.conditions, []) + def test_end_support_for_version_0(self): + if time.gmtime().tm_year == 2021: + self.fail(""" +Happy new year 2021! + +Now remove support for version==None settings in +SelectRowsContextHandler.decode_setting and SelectRowsContextHandler.match, +and uncomment OWSelectRows.migrate. + +In tests, uncomment test_migration_to_version_1, +and remove test_support_old_settings and this test. + +Basically, revert this commit. +""") def widget_with_context(self, domain, conditions): ch = SelectRowsContextHandler()