Skip to content
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

Add __init__ and check_items to Collection #778

Merged
merged 10 commits into from
Oct 9, 2024
Merged
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
64 changes: 45 additions & 19 deletions src/tests/test_iterables.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
text,
)
from pytest import mark, param, raises
from typing_extensions import override

from utilities.hypothesis import sets_fixed_length
from utilities.iterables import (
Expand All @@ -35,6 +36,7 @@
Collection,
EnsureIterableError,
EnsureIterableNotStrError,
MaybeIterable,
OneEmptyError,
OneNonUniqueError,
OneStrError,
Expand Down Expand Up @@ -537,26 +539,17 @@ def test_hash(self) -> None:
collection = Collection(map(_Item, range(3)))
_ = hash(collection)

def test_init_one_singleton(self) -> None:
collection = Collection(_Item(1))
assert isinstance(collection, Collection)
assert len(collection) == 1
assert one(collection) == _Item(1)
def test_init(self) -> None:
class SubCollection(Collection[_Item]):
@override
def __init__(self, *item_or_items: MaybeIterable[_Item]) -> None:
super().__init__(*item_or_items)
if any(item.n >= 1 for item in self):
msg = "n >= 1 is not permitted"
raise ValueError(msg)

def test_init_one_iterable(self) -> None:
collection = Collection(map(_Item, range(3)))
assert isinstance(collection, Collection)
assert len(collection) == 3

def test_init_many_singletons(self) -> None:
collection = Collection(_Item(1), _Item(2), _Item(3))
assert isinstance(collection, Collection)
assert len(collection) == 3

def test_init_many_iterables(self) -> None:
collection = Collection(map(_Item, range(3)), map(_Item, range(3)))
assert isinstance(collection, Collection)
assert len(collection) == 3
with raises(ValueError, match="n >= 1 is not permitted"):
_ = SubCollection(map(_Item, range(3)))

def test_iter(self) -> None:
collection = Collection(map(_Item, range(3)))
Expand All @@ -578,6 +571,39 @@ def test_map_return_different_type(self) -> None:
expected = Collection(range(3))
assert result == expected

def test_new_one_singleton(self) -> None:
collection = Collection(_Item(1))
assert isinstance(collection, Collection)
assert len(collection) == 1
assert one(collection) == _Item(1)

def test_new_one_iterable(self) -> None:
collection = Collection(map(_Item, range(3)))
assert isinstance(collection, Collection)
assert len(collection) == 3

def test_new_many_singletons(self) -> None:
collection = Collection(_Item(1), _Item(2), _Item(3))
assert isinstance(collection, Collection)
assert len(collection) == 3

def test_new_many_iterables(self) -> None:
collection = Collection(map(_Item, range(3)), map(_Item, range(3)))
assert isinstance(collection, Collection)
assert len(collection) == 3

def test_new_check_items(self) -> None:
class SubCollection(Collection[_Item]):
@classmethod
@override
def check_items(cls, items: Iterable[_Item]) -> None:
if any(item.n >= 1 for item in items):
msg = "n >= 1 is not permitted"
raise ValueError(msg)

with raises(ValueError, match="n >= 1 is not permitted"):
_ = SubCollection(map(_Item, range(3)))

def test_or_singleton(self) -> None:
collection = Collection(map(_Item, range(3)))
result = collection | _Item(3)
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "0.58.13"
__version__ = "0.58.14"
9 changes: 8 additions & 1 deletion src/utilities/iterables.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,14 @@ def __lt__(self, other: Any, /) -> bool: ... # pragma: no cover
class Collection(frozenset[_TSupportsHashAndSort]):
"""A collection of hashable, sortable items."""

@override
def __new__(cls, *item_or_items: MaybeIterable[_TSupportsHashAndSort]) -> Self:
items = list(chain(*map(always_iterable, item_or_items)))
cls.check_items(items)
return super().__new__(cls, items)

def __init__(self, *item_or_items: MaybeIterable[_TSupportsHashAndSort]) -> None:
_ = item_or_items

@override
def __and__(self, other: MaybeIterable[_TSupportsHashAndSort], /) -> Self:
if isinstance(other, type(self)):
Expand Down Expand Up @@ -572,6 +575,10 @@ def __sub__(
other = cast(Iterable[_TSupportsHashAndSort], other)
return self.__sub__(type(self)(other))

@classmethod
def check_items(cls, items: Iterable[_TSupportsHashAndSort], /) -> None:
_ = items

def filter(self, func: Callable[[_TSupportsHashAndSort], bool], /) -> Self:
return type(self)(filter(func, self))

Expand Down