Skip to content

Commit

Permalink
Revert "feat: make arr a subclass of list" (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Dec 11, 2024
2 parents 6f82538 + a335413 commit 9b143f5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 54 deletions.
1 change: 1 addition & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ select = [
"COM",
"D417",
"E",
"ERA",
"F",
"I",
"ICN",
Expand Down
11 changes: 0 additions & 11 deletions integator.toml

This file was deleted.

26 changes: 13 additions & 13 deletions iterpy/arr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from itertools import islice
from typing import TYPE_CHECKING, Generic, Sequence, SupportsIndex, TypeVar, overload
from typing import TYPE_CHECKING, Generic, Sequence, TypeVar, overload

if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Iterator
Expand All @@ -12,7 +12,7 @@
S = TypeVar("S")


class Arr(Generic[T], list[T]):
class Arr(Generic[T]):
def __init__(self, iterable: Iterable[T]) -> None:
self._iter = list(iterable)
self._current_index: int = 0
Expand All @@ -36,11 +36,11 @@ def __next__(self) -> T:
return item

@overload
def __getitem__(self, index: SupportsIndex) -> T: ...
def __getitem__(self, index: int) -> T: ...
@overload
def __getitem__(self, index: slice) -> Arr[T]: ...

def __getitem__(self, index: SupportsIndex | slice) -> T | Arr[T]:
def __getitem__(self, index: int | slice) -> T | Arr[T]:
if isinstance(index, int) and index >= 0:
try:
return next(islice(self._iter, index, index + 1))
Expand All @@ -63,8 +63,8 @@ def __eq__(self, other: object) -> bool:
def reduce(self, func: Callable[[T, T], T]) -> T:
return self.lazy().reduce(func)

def len(self) -> int:
return self.lazy().len()
def count(self) -> int:
return self.lazy().count()

### Output
def to_list(self) -> list[T]:
Expand Down Expand Up @@ -138,43 +138,43 @@ def flatten(self: Arr[Iterable[S]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[Iterable[S] | S]) -> Arr[S]: ...

# Iterator[S]
# Iterator[S] # noqa: ERA001
@overload
def flatten(self: Arr[Iterator[S]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[Iterator[S] | S]) -> Arr[S]: ...

# tuple[S, ...]
# tuple[S, ...] # noqa: ERA001
@overload
def flatten(self: Arr[tuple[S, ...]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[tuple[S, ...] | S]) -> Arr[S]: ...

# Sequence[S]
# Sequence[S] # noqa: ERA001
@overload
def flatten(self: Arr[Sequence[S]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[Sequence[S] | S]) -> Arr[S]: ...

# list[S]
# list[S] # noqa: ERA001
@overload
def flatten(self: Arr[list[S]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[list[S] | S]) -> Arr[S]: ...

# set[S]
# set[S] # noqa: ERA001
@overload
def flatten(self: Arr[set[S]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[set[S] | S]) -> Arr[S]: ...

# frozenset[S]
# frozenset[S] # noqa: ERA001
@overload
def flatten(self: Arr[frozenset[S]]) -> Arr[S]: ...
@overload
def flatten(self: Arr[frozenset[S] | S]) -> Arr[S]: ...

# Arr[S]
# Arr[S] # noqa: ERA001
@overload
def flatten(self: Arr[Arr[S]]) -> Arr[S]: ...
@overload
Expand Down
16 changes: 8 additions & 8 deletions iterpy/iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __eq__(self, other: object) -> bool:
def reduce(self, func: Callable[[T, T], T]) -> T:
return reduce(func, self._iterator)

def len(self) -> int:
def count(self) -> int:
return sum(1 for _ in self._iterator)

### Output
Expand Down Expand Up @@ -157,43 +157,43 @@ def flatten(self: Iter[Iterable[S]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[Iterable[S] | S]) -> Iter[S]: ...

# Iterator[S]
# Iterator[S] # noqa: ERA001
@overload
def flatten(self: Iter[Iterator[S]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[Iterator[S] | S]) -> Iter[S]: ...

# tuple[S, ...]
# tuple[S, ...] # noqa: ERA001
@overload
def flatten(self: Iter[tuple[S, ...]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[tuple[S, ...] | S]) -> Iter[S]: ...

# Sequence[S]
# Sequence[S] # noqa: ERA001
@overload
def flatten(self: Iter[Sequence[S]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[Sequence[S] | S]) -> Iter[S]: ...

# list[S]
# list[S] # noqa: ERA001
@overload
def flatten(self: Iter[list[S]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[list[S] | S]) -> Iter[S]: ...

# set[S]
# set[S] # noqa: ERA001
@overload
def flatten(self: Iter[set[S]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[set[S] | S]) -> Iter[S]: ...

# frozenset[S]
# frozenset[S] # noqa: ERA001
@overload
def flatten(self: Iter[frozenset[S]]) -> Iter[S]: ...
@overload
def flatten(self: Iter[frozenset[S] | S]) -> Iter[S]: ...

# Iter[S]
# Iter[S] # noqa: ERA001
@overload
def flatten(self: Iter[Iter[S]]) -> Iter[S]: ...
@overload
Expand Down
14 changes: 2 additions & 12 deletions iterpy/test_arr.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,10 @@ def test_reduce():

def test_count():
iterator = Arr([1, 2])
result: int = iterator.len()
result: int = iterator.count()
assert result == 2


def func(listicle: list[str]) -> list[str]:
return listicle


def test_mul():
iterator = Arr(["val"])
result: list[str] = func(iterator)
assert result[0] == "val"


def test_grouped_filter():
iterator = Arr([1, 2, 3, 4])

Expand Down Expand Up @@ -199,7 +189,7 @@ def test_flatten_str(self):

def test_flatten_includes_primitives(self):
test_input: list[str | list[int] | None] = ["first", [2], None]
result: Arr[int | str | None] = Arr(test_input).flatten() # type: ignore # TODO: Would love to see a fix for this
result: Arr[int | str | None] = Arr(test_input).flatten() # type: ignore # TODO: Would love to see a fix for this
assert result.to_list() == ["first", 2, None]

def test_flatten_removes_empty_iterators(self):
Expand Down
4 changes: 2 additions & 2 deletions iterpy/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_reduce():

def test_count():
iterator = Iter([1, 2])
result: int = iterator.len()
result: int = iterator.count()
assert result == 2


Expand Down Expand Up @@ -188,7 +188,7 @@ def test_flatten_str(self):

def test_flatten_includes_primitives(self):
test_input: list[str | list[int] | None] = ["first", [2], None]
result: Iter[int | str | None] = Iter(test_input).flatten() # type: ignore # TODO: Would love to see a fix for this
result: Iter[int | str | None] = Iter(test_input).flatten() # type: ignore # TODO: Would love to see a fix for this
assert result.to_list() == ["first", 2, None]

def test_flatten_removes_empty_iterators(self):
Expand Down
12 changes: 4 additions & 8 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"exclude": [
"**/node_modules",
"**/__pycache__",
"**/.*",
"build"
".*venv*"
],
"typeCheckingMode": "off",
"strict": [
"**/*.py"
]
"pythonPlatform": "Darwin",
"reportMissingTypeStubs": false,
"typeCheckingMode": "strict"
}

0 comments on commit 9b143f5

Please sign in to comment.