Skip to content

Commit 6aa02e4

Browse files
committed
[util] Allow list in cardinality
1 parent 27cb745 commit 6aa02e4

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

odml/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ def format_cardinality(in_val):
3131
return None
3232

3333
# Catch tuple edge cases (0, 0); (None, None); (0, None); (None, 0)
34-
if isinstance(in_val, tuple) and len(in_val) > 1 and not in_val[0] and not in_val[1]:
34+
if isinstance(in_val, (tuple, list)) and len(in_val) == 2 and not in_val[0] and not in_val[1]:
3535
return None
3636

3737
# Providing a single integer sets the maximum value in a tuple.
3838
if isinstance(in_val, int) and in_val > 0:
3939
return None, in_val
4040

41-
# Only integer 2-tuples of the format '(min, max)' are supported to set the cardinality
42-
if isinstance(in_val, tuple) and len(in_val) == 2:
41+
# Integer 2-tuples of the format '(min, max)' are supported to set the cardinality.
42+
# Also support lists with a length of 2 without advertising it.
43+
if isinstance(in_val, (tuple, list)) and len(in_val) == 2:
4344
v_min = in_val[0]
4445
v_max = in_val[1]
4546

test/test_util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def test_format_cardinality(self):
3434
set_val = (2, 3)
3535
self.assertEqual(format_cardinality(set_val), set_val)
3636

37+
# Test list simple list set
38+
set_val = [2, None]
39+
self.assertEqual(format_cardinality(set_val), tuple(set_val))
40+
set_val = [None, 2]
41+
self.assertEqual(format_cardinality(set_val), tuple(set_val))
42+
set_val = [2, 3]
43+
self.assertEqual(format_cardinality(set_val), tuple(set_val))
44+
3745
# Test exact value tuple set
3846
set_val = (5, 5)
3947
self.assertEqual(format_cardinality(set_val), set_val)
@@ -42,6 +50,15 @@ def test_format_cardinality(self):
4250
with self.assertRaises(ValueError):
4351
format_cardinality("a")
4452

53+
with self.assertRaises(ValueError):
54+
format_cardinality([1])
55+
56+
with self.assertRaises(ValueError):
57+
format_cardinality([1, 2, 3])
58+
59+
with self.assertRaises(ValueError):
60+
format_cardinality({1: 2, 3: 4})
61+
4562
with self.assertRaises(ValueError):
4663
format_cardinality(-1)
4764

0 commit comments

Comments
 (0)