Skip to content

Commit

Permalink
Fix Array iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Dec 6, 2024
1 parent f822bea commit 74d7b46
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
run: pytest --color=yes -v tests

- name: Run code coverage
if: ${{ (matrix.python-version == '3.12') && (matrix.os == 'ubuntu') }}
if: ${{ (matrix.python-version == '3.13') && (matrix.os == 'ubuntu') }}
run: |
coverage run -m pytest tests
coverage report --show-missing --fail-under=100
3 changes: 3 additions & 0 deletions python/pycrdt/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ def __init__(self, array: Array):
self.length = len(array)
self.idx = 0

def __iter__(self) -> ArrayIterator:
return self

def __next__(self) -> Any:
if self.idx == self.length:
raise StopIteration
Expand Down
21 changes: 3 additions & 18 deletions python/pycrdt/_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, cast
from typing import TYPE_CHECKING, Any, Callable, Iterator, cast

from ._base import BaseEvent, BaseType, base_types, event_types
from ._pycrdt import Subscription
Expand Down Expand Up @@ -50,7 +50,7 @@ def _init(self, value: str | None) -> None:
def _get_or_insert(self, name: str, doc: Doc) -> _Text:
return doc._doc.get_or_insert_text(name)

def __iter__(self) -> TextIterator:
def __iter__(self) -> Iterator[str]:
"""
```py
Doc()["text"] = text = Text("***")
Expand All @@ -61,7 +61,7 @@ def __iter__(self) -> TextIterator:
Returns:
An iterable over the characters of the text.
"""
return TextIterator(self)
return iter(str(self))

def __contains__(self, item: str) -> bool:
"""
Expand Down Expand Up @@ -318,20 +318,5 @@ class TextEvent(BaseEvent):
__slots__ = "target", "delta", "path"


class TextIterator:
def __init__(self, text: Text):
self.text = text
self.length = len(text)
self.idx = 0

def __next__(self) -> str:
if self.idx == self.length:
raise StopIteration

res = self.text[self.idx]
self.idx += 1
return res


base_types[_Text] = Text
event_types[_TextEvent] = TextEvent
6 changes: 6 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def callback(events, event):
)


def test_iterate():
doc = Doc()
doc["array"] = array = Array([0, 2, 1])
assert [val for val in array] == [0, 2, 1]


def test_str():
doc = Doc()
map2 = Map({"key": "val"})
Expand Down
9 changes: 9 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
punct = "!"


def test_iterate():
doc = Doc()
doc["text"] = text = Text("abc")
assert [char for char in text] == ["a", "b", "c"]


def test_str():
doc1 = Doc()
text1 = Text()
Expand Down Expand Up @@ -101,6 +107,9 @@ def test_slice():
doc = Doc()
doc["text"] = text = Text(hello)

for i, c in enumerate(hello):
assert text[i] == c

with pytest.raises(RuntimeError) as excinfo:
text[1::2] = "a"
assert str(excinfo.value) == "Step not supported"
Expand Down

0 comments on commit 74d7b46

Please sign in to comment.