Skip to content

Commit 1fd9d76

Browse files
committed
Re-add TupleList to DiscreteVariable
1 parent 62e5c69 commit 1fd9d76

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Orange/data/tests/test_variable.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
33
# pylint: disable=protected-access
4-
4+
import os
55
import sys
66
import math
77
import unittest
88
import pickle
99
import pkgutil
10+
import warnings
1011
from datetime import datetime, timezone
1112

1213
from io import StringIO
@@ -16,7 +17,7 @@
1617

1718
import Orange
1819
from Orange.data import Variable, ContinuousVariable, DiscreteVariable, \
19-
StringVariable, TimeVariable, Unknown, Value
20+
StringVariable, TimeVariable, Unknown, Value, Table
2021
from Orange.data.io import CSVReader
2122
from Orange.preprocess.transformation import Identity
2223
from Orange.tests.base import create_pickling_tests
@@ -500,6 +501,25 @@ def test_remove_ordered(self):
500501
"""
501502
self.assertLess(Orange.__version__, "3.29.0")
502503

504+
@staticmethod
505+
def test_pickle_backward_compatibility():
506+
"""
507+
Test that pickle made with an older version of Orange are correctly
508+
loaded after changes in DiscreteVariable
509+
"""
510+
with warnings.catch_warnings():
511+
# travis/gh-action tests change OrangeDeprecationWarning to error
512+
# temporary disable it
513+
warnings.simplefilter('default', OrangeDeprecationWarning)
514+
this_dir = os.path.dirname(os.path.realpath(__file__))
515+
datasets_dir = os.path.join(
516+
this_dir, "..", "..", "tests", "datasets"
517+
)
518+
# pickle with values as list
519+
Table(os.path.join(datasets_dir, "sailing-orange-3-20.pkl"))
520+
# pickle with values as tuple list
521+
Table(os.path.join(datasets_dir, "iris-orange-3-25.pkl"))
522+
503523

504524
@variabletest(ContinuousVariable)
505525
class TestContinuousVariable(VariableTest):

Orange/data/variable.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ def copy(self, compute_value=Variable._CopyComputeValue,
602602
return var
603603

604604

605+
TupleList = tuple # backward compatibility (for pickled table)
606+
607+
605608
class DiscreteVariable(Variable):
606609
"""
607610
Descriptor for symbolic, discrete variables. Values of discrete variables
@@ -622,7 +625,7 @@ def __init__(
622625
*, sparse=False
623626
):
624627
""" Construct a discrete variable descriptor with the given values. """
625-
values = tuple(values) # some people (including me) pass a generator
628+
values = TupleList(values) # some people pass a generator
626629
if not all(isinstance(value, str) for value in values):
627630
raise TypeError("values of DiscreteVariables must be strings")
628631

8.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)