Skip to content

Commit 28e99c0

Browse files
committed
Sorted keys and items views set-operations return sorted sets
1 parent 9b2d38c commit 28e99c0

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

sortedcontainers/sorteddict.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from collections import ItemsView, KeysView, ValuesView, Sequence
2020

2121
from .sortedlist import SortedList, recursive_repr
22+
from .sortedset import SortedSet
2223

2324

2425
class SortedDict(dict):
@@ -554,7 +555,7 @@ def _view_delitem(self, index):
554555
555556
``view.__delitem__(index)`` <==> ``del view[index]``
556557
557-
Support slicing.
558+
Supports slicing.
558559
559560
Runtime complexity: `O(log(n))` -- approximate.
560561
@@ -598,6 +599,11 @@ class SortedKeysView(KeysView, Sequence):
598599
__slots__ = ()
599600

600601

602+
@classmethod
603+
def _from_iterable(self, it):
604+
return SortedSet(it)
605+
606+
601607
def __getitem__(self, index):
602608
"""Lookup key at `index` in sorted keys views.
603609
@@ -642,6 +648,11 @@ class SortedItemsView(ItemsView, Sequence):
642648
__slots__ = ()
643649

644650

651+
@classmethod
652+
def _from_iterable(self, it):
653+
return SortedSet(it)
654+
655+
645656
def __getitem__(self, index):
646657
"""Lookup item at `index` in sorted items view.
647658

tests/test_coverage_sorteddict.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ def test_keysview():
377377
assert keys <= that_keys
378378
assert keys >= that_keys
379379

380-
assert sorted(keys & that_keys) == [val for val, pos in mapping]
381-
assert sorted(keys | that_keys) == [val for val, pos in mapping]
382-
assert sorted(keys - that_keys) == []
383-
assert sorted(keys ^ that_keys) == []
380+
assert list(keys & that_keys) == [val for val, pos in mapping]
381+
assert list(keys | that_keys) == [val for val, pos in mapping]
382+
assert list(keys - that_keys) == []
383+
assert list(keys ^ that_keys) == []
384384

385385
keys = SortedDict(mapping[:2]).keys()
386386
assert repr(keys) == "SortedKeysView(SortedDict({'a': 0, 'b': 1}))"
@@ -447,10 +447,10 @@ def test_itemsview():
447447
assert items <= that_items
448448
assert items >= that_items
449449

450-
assert sorted(items & that_items) == mapping
451-
assert sorted(items | that_items) == mapping
452-
assert sorted(items - that_items) == []
453-
assert sorted(items ^ that_items) == []
450+
assert list(items & that_items) == mapping
451+
assert list(items | that_items) == mapping
452+
assert list(items - that_items) == []
453+
assert list(items ^ that_items) == []
454454

455455
items = SortedDict(mapping[:2]).items()
456456
assert repr(items) == "SortedItemsView(SortedDict({'a': 0, 'b': 1}))"

0 commit comments

Comments
 (0)