From f6b7dde95ebc4782b304a9175e4c7df34cb85f17 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Primo=C5=BE=20Godec?=
Date: Thu, 28 May 2020 14:05:49 +0200
Subject: [PATCH] OWSelectColumns: Fix sorting for cases when new column added
to domain
---
Orange/widgets/data/owselectcolumns.py | 7 ++++---
Orange/widgets/data/tests/test_owselectcolumns.py | 15 +++++++++++++++
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/Orange/widgets/data/owselectcolumns.py b/Orange/widgets/data/owselectcolumns.py
index f511fba7747..312dad4d08a 100644
--- a/Orange/widgets/data/owselectcolumns.py
+++ b/Orange/widgets/data/owselectcolumns.py
@@ -362,9 +362,10 @@ def set_data(self, data=None):
all_vars = data.domain.variables + data.domain.metas
def attrs_for_role(role):
- return [attr for _, attr in sorted(
- (domain_hints[attr][1], attr)
- for attr in all_vars if domain_hints[attr][0] == role)]
+ selected_attrs = [
+ attr for attr in all_vars if domain_hints[attr][0] == role
+ ]
+ return sorted(selected_attrs, key=lambda attr: domain_hints[attr][1])
domain = data.domain
domain_hints = {}
diff --git a/Orange/widgets/data/tests/test_owselectcolumns.py b/Orange/widgets/data/tests/test_owselectcolumns.py
index 76a1dd2d29e..5a7e87cf7bb 100644
--- a/Orange/widgets/data/tests/test_owselectcolumns.py
+++ b/Orange/widgets/data/tests/test_owselectcolumns.py
@@ -2,6 +2,7 @@
from unittest import TestCase
from unittest.mock import Mock
+import numpy as np
from AnyQt.QtCore import QMimeData, QPoint, Qt
from AnyQt.QtGui import QDragEnterEvent
@@ -396,3 +397,17 @@ def test_summary(self):
self.assertEqual(input_sum.call_args[0][0].brief, "")
output_sum.assert_called_once()
self.assertEqual(output_sum.call_args[0][0].brief, "")
+
+ def test_domain_new_feature(self):
+ """ Test scenario when new attribute is added at position 0 """
+ data = Table("iris")
+ self.send_signal(self.widget.Inputs.data, data)
+
+ data1 = Table(
+ Domain(
+ (ContinuousVariable("a"),) + data.domain.attributes,
+ data.domain.class_var),
+ np.hstack((np.ones((len(data), 1)), data.X)),
+ data.Y
+ )
+ self.send_signal(self.widget.Inputs.data, data1)