grantjenks / python-sortedcontainers

Python Sorted Container Types: Sorted List, Sorted Dict, and Sorted Set
http://www.grantjenks.com/docs/sortedcontainers/
Other
3.51k stars 203 forks source link

SortedList.remove() incorrectly raises ValueError #223

Closed figura-equi closed 1 year ago

figura-equi commented 1 year ago
from sortedcontainers import SortedList

class MySortableClass:

    def __init__(self, value):
        self.value = value

    def __lt__(self, other):
        return self.value < other.value

def test_sorted_list_remove():
    sorted_list = SortedList()
    item_1 = MySortableClass(1)
    item_2 = MySortableClass(1)

    sorted_list.add(item_1)
    sorted_list.add(item_2)
    sorted_list.remove(item_2)

Fails with ValueError (not in list), because remove finds the position 0 and then sees the object on position 0 is different.

grantjenks commented 1 year ago

You haven’t defined equality correctly. See https://grantjenks.com/docs/sortedcontainers/introduction.html#caveats