Skip to content

Simplify FrozenIndexSet #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions orderedsets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,33 +434,5 @@ class FrozenIndexSet(FrozenOrderedSet[T_cov]):
.. automethod:: __getitem__
"""

def __getitem__(self, index: int | slice) -> T_cov | list[T_cov]:
"""Return the element at *index* or a list of elements for a slice.

.. doctest::

>>> fiset = FrozenIndexSet(["a", "b", "c", "d", "e", "f", "g", "h", "i"])
>>> fiset[0]
'a'
>>> fiset[-1]
'i'
>>> fiset[-2:-8:-2]
['h', 'f', 'd']
"""
if isinstance(index, int):
if index < 0:
index = len(self) + index

if index >= len(self) or index < 0:
raise IndexError("Index out of range.")

from itertools import islice
return next(islice(self._dict.keys(), index, index + 1))

elif isinstance(index, slice):
from more_itertools import islice_extended
return list(
islice_extended(self._dict.keys(), index.start, index.stop, index.step))

else:
raise TypeError("Index must be an integer or slice.")
# Can't derive from IndexSet because that would add the mutating methods
__getitem__ = IndexSet.__getitem__
10 changes: 5 additions & 5 deletions test/test_orderedsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

import pytest

from orderedsets import FrozenOrderedSet, OrderedSet
from orderedsets import FrozenIndexSet, FrozenOrderedSet, IndexSet, OrderedSet

T = TypeVar("T")

set_types = (OrderedSet, FrozenOrderedSet, set, frozenset)
ordered_set_types = (OrderedSet, FrozenOrderedSet)
mutable_set_types = (OrderedSet, set)
immutable_set_types = (FrozenOrderedSet, frozenset)
set_types = (OrderedSet, FrozenOrderedSet, set, frozenset, IndexSet, FrozenIndexSet)
ordered_set_types = (OrderedSet, FrozenOrderedSet, IndexSet, FrozenIndexSet)
mutable_set_types = (OrderedSet, set, IndexSet)
immutable_set_types = (FrozenOrderedSet, frozenset, FrozenIndexSet)

T_set = Union[Type[OrderedSet[T]], Type[FrozenOrderedSet[T]],
Type[Set[T]], Type[FrozenSet[T]]]
Expand Down
Loading