From 2c60ff31ef64ea6511f14148a26e96d54d835204 Mon Sep 17 00:00:00 2001 From: Andreja Kovacic Date: Sat, 16 May 2020 18:30:45 +0200 Subject: [PATCH] Domain: throw exception if all var names are not unique --- Orange/data/domain.py | 6 ++++++ Orange/tests/test_domain.py | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Orange/data/domain.py b/Orange/data/domain.py index e4082c0a250..1918b21b3e7 100644 --- a/Orange/data/domain.py +++ b/Orange/data/domain.py @@ -148,6 +148,12 @@ def __init__(self, attributes, class_vars=None, metas=None, source=None): raise TypeError( "descriptors must be instances of Variable, " "not '%s'" % type(var).__name__) + + names = [var.name for var in chain(attributes, class_vars, metas)] + if len(names) != len(set(names)): + print(names) + raise Exception('All the variables in the domain should have' + ' unique names.') # Store everything self.attributes = tuple(attributes) diff --git a/Orange/tests/test_domain.py b/Orange/tests/test_domain.py index aa1d5d6f1c9..bbf95251b72 100644 --- a/Orange/tests/test_domain.py +++ b/Orange/tests/test_domain.py @@ -120,11 +120,11 @@ def test_init_source_class(self): def test_init_metas(self): attributes = (age, gender, income) metas = (ssn, race) - d = Domain(attributes, race, metas=metas) - self.assertEqual(d.variables, attributes + (race, )) + d = Domain(attributes, education, metas=metas) + self.assertEqual(d.variables, attributes + (education, )) self.assertEqual(d.attributes, attributes) - self.assertEqual(d.class_var, race) - self.assertEqual(d.class_vars, (race, )) + self.assertEqual(d.class_var, education) + self.assertEqual(d.class_vars, (education, )) self.assertEqual(d.metas, metas) def test_from_numpy_names(self): @@ -230,7 +230,7 @@ def test_index(self): self.assertEqual(d.index(idx), var) def test_get_item_slices(self): - d = Domain((age, gender, income, race), metas=(ssn, race)) + d = Domain((age, gender, income, race), metas=(ssn, education)) self.assertEqual(d[:2], (age, gender)) self.assertEqual(d[1:3], (gender, income)) self.assertEqual(d[2:], (income, race)) @@ -377,19 +377,19 @@ def test_has_time(self): def test_get_conversion(self): compute_value = lambda: 42 - new_income = income.copy(compute_value=compute_value) + new_income = income.copy(compute_value=compute_value, name='new_income') d = Domain((age, gender, income), metas=(ssn, race)) - e = Domain((gender, race), None, metas=(age, gender, ssn)) - f = Domain((gender,), (race, income), metas=(age, income, ssn)) + e = Domain((gender, race), None, metas=(age, income, ssn)) + f = Domain((gender,), (race, income), metas=(age, incomeA, ssn)) g = Domain((), metas=(age, gender, ssn)) - h = Domain((gender,), (race, new_income), metas=(age, new_income, ssn)) + h = Domain((gender,), (race, income), metas=(age, new_income, ssn)) for conver, domain, attr, class_vars, metas in ( - (d, e, [1, -2], [], [0, 1, -1]), - (d, f, [1], [-2, 2], [0, 2, -1]), + (d, e, [1, -2], [], [0, 2, -1]), + (d, f, [1], [-2, 2], [0, None, -1]), (f, g, [], [], [-1, 0, -3]), - (g, h, [-2], [None, compute_value], [-1, compute_value, -3])): + (g, h, [-2], [None, None], [-1, compute_value, -3])): to_domain = DomainConversion(conver, domain) self.assertIs(to_domain.source, conver) self.assertEqual(to_domain.attributes, attr)