diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b9d60e2..7b0e89a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/python/pycrdt/_array.py b/python/pycrdt/_array.py index 24a5195..3e146e2 100644 --- a/python/pycrdt/_array.py +++ b/python/pycrdt/_array.py @@ -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 diff --git a/python/pycrdt/_text.py b/python/pycrdt/_text.py index 459677e..d6da719 100644 --- a/python/pycrdt/_text.py +++ b/python/pycrdt/_text.py @@ -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 @@ -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("***") @@ -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: """ @@ -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 diff --git a/tests/test_array.py b/tests/test_array.py index 90a5b2f..9580ccf 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -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"}) diff --git a/tests/test_text.py b/tests/test_text.py index c55a9c8..1c8e267 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -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() @@ -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"