Skip to content

Commit 61f7142

Browse files
committed
[property] Properly handle cardinality setter
1 parent d11c7a9 commit 61f7142

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

odml/property.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,41 @@ def val_cardinality(self, new_value):
540540
:param new_value: Can be either 'None', a positive integer, which will set the maximum
541541
or an integer 2-tuple of the format '(min, max)'.
542542
"""
543-
self._val_cardinality = new_value
543+
invalid_input = False
544+
545+
# Empty values reset the cardinality to None.
546+
if not new_value or new_value == (None, None):
547+
self._val_cardinality = None
548+
549+
# Providing a single integer sets the maximum value in a tuple.
550+
elif isinstance(new_value, int) and new_value > 0:
551+
self._val_cardinality = (None, new_value)
552+
553+
# Only integer 2-tuples of the format '(min, max)' are supported to set the cardinality
554+
elif isinstance(new_value, tuple) and len(new_value) == 2:
555+
v_min = new_value[0]
556+
v_max = new_value[1]
557+
558+
min_int = isinstance(v_min, int) and v_min >= 0
559+
max_int = isinstance(v_max, int) and v_max >= 0
560+
561+
if max_int and min_int and v_max > v_min:
562+
self._val_cardinality = (v_min, v_max)
563+
564+
elif max_int and not v_min:
565+
self._val_cardinality = (None, v_max)
566+
567+
elif min_int and not v_max:
568+
self._val_cardinality = (v_min, None)
569+
570+
else:
571+
invalid_input = True
572+
else:
573+
invalid_input = True
574+
575+
if invalid_input:
576+
msg = "Can only assign single int or int-tuples of the format '(min, max)'"
577+
raise ValueError(msg)
544578

545579
def remove(self, value):
546580
"""

0 commit comments

Comments
 (0)