Skip to content

Commit 4ccaaec

Browse files
committed
itemmodels.AbstractSortTableModel: Fix mapFromSourceRows
1 parent 9225e8d commit 4ccaaec

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Orange/widgets/utils/itemmodels.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ def mapToSourceRows(self, rows):
127127
Source rows matching input rows. If they are the same,
128128
simply input `rows` is returned.
129129
"""
130-
if self.__sortInd is not None:
130+
# self.__sortInd[rows] fails if `rows` is an empty list or array
131+
if self.__sortInd is not None \
132+
and (isinstance(rows, (int, type(Ellipsis)))
133+
or len(rows)):
131134
new_rows = self.__sortInd[rows]
132135
if rows is Ellipsis:
133136
new_rows.setflags(write=False)

Orange/widgets/utils/tests/test_itemmodels.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from unittest import TestCase
55

6+
import numpy as np
7+
68
from AnyQt.QtCore import Qt
79

810
from Orange.data import Domain, ContinuousVariable, DiscreteVariable
@@ -133,6 +135,24 @@ def test_sorting(self):
133135
self.model.sort(1, Qt.DescendingOrder)
134136
self.assertSequenceEqual(self.model.mapToSourceRows(...).tolist(), [0, 1])
135137

138+
def test_mapToSourceRows(self):
139+
self.model.sort(1, Qt.AscendingOrder)
140+
self.assertSequenceEqual(
141+
self.model.mapToSourceRows(...).tolist(),
142+
[1, 0])
143+
self.assertEqual(
144+
self.model.mapToSourceRows(1).tolist(),
145+
0)
146+
self.assertSequenceEqual(
147+
self.model.mapToSourceRows([1, 0]).tolist(),
148+
[0, 1])
149+
self.assertSequenceEqual(
150+
self.model.mapToSourceRows([]),
151+
[])
152+
self.assertSequenceEqual(
153+
self.model.mapToSourceRows(np.array([], dtype=int)).tolist(),
154+
[])
155+
136156

137157
class TestPyListModel(TestCase):
138158
@classmethod

0 commit comments

Comments
 (0)