Range is not comparable with the other Ranges in this RangeSet
biggates opened this issue · 1 comments
biggates commented
Consider the following python snippet:
from ranges import Range, RangeSet
result = RangeSet()
def add(rangeset: RangeSet, start: int, end: int):
r = Range(start=start, end=end)
print('Range to add :', r)
rangeset.add(r)
print('RangeSet after add :', rangeset)
print('------')
# now result is empty
add(result, 100, 200)
# now result = {[100, 200)}
add(result, 200, 300)
# this works fine, results in {[100, 300)}
add(result, 400, 500)
# now result is correctly assigned with {[100, 300), [400, 500)}
# expected result is "{[100, 300), [400, 600)}", however this will break
add(result, 500, 600)
which returns:
Range to add : [100, 200)
RangeSet after add : {[100, 200)}
------
Range to add : [200, 300)
RangeSet after add : {[100, 300)}
------
Range to add : [400, 500)
RangeSet after add : {[100, 300), [400, 500)}
------
Range to add : [500, 600)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\miniconda3\lib\site-packages\ranges\ranges.py in union(self, rng)
241 if not isinstance(rng, Range):
--> 242 rng = Range(rng)
243 except ValueError:
~\miniconda3\lib\site-packages\ranges\ranges.py in __init__(self, *args, **kwargs)
187 else:
--> 188 raise ValueError(f"cannot construct a new Range from an object of type '{type(rng)}'")
189 else:
ValueError: cannot construct a new Range from an object of type '<class 'ranges._helper._LinkedList.Node'>'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
~\miniconda3\lib\site-packages\ranges\ranges.py in add(self, rng)
696 temp_ranges.pop_before(inserted_node)
--> 697 prev_union = inserted_node.value.union(inserted_node.prev) if inserted_node.prev else None
698 # merge this range with the next range(s)
~\miniconda3\lib\site-packages\ranges\ranges.py in union(self, rng)
243 except ValueError:
--> 244 raise TypeError("Cannot merge a Range with a non-Range")
245 # do the ranges overlap?
TypeError: Cannot merge a Range with a non-Range
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-10-394c1b3520d9> in <module>
13 add(result, 200, 300)
14 add(result, 400, 500)
---> 15 add(result, 500, 600)
<ipython-input-10-394c1b3520d9> in add(rangeset, start, end)
6 r = Range(start=start, end=end)
7 print('Range to add :', r)
----> 8 rangeset.add(r)
9 print('RangeSet after add :', rangeset)
10 print('------')
~\miniconda3\lib\site-packages\ranges\ranges.py in add(self, rng)
704 next_union = inserted_node.value.union(inserted_node.next) if inserted_node.next else None
705 except TypeError:
--> 706 raise TypeError(f"Range '{rng}' is not comparable with the other Ranges in this RangeSet")
707 # apply changes
708 self._ranges = temp_ranges
TypeError: Range '[500, 600)' is not comparable with the other Ranges in this RangeSet
molszowy commented
Hi! I encountered the same issue. It seems to be a bug @ line 697 so the solution is:
prev_union = inserted_node.value.union(inserted_node.prev.value) if inserted_node.prev else None
@Superbird11 Can you push a quick fix ?