-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy StreamedStr type hints. Improve README example. (#7)
- Loading branch information
1 parent
49b1aed
commit 08ad9c0
Showing
3 changed files
with
66 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
from typing import AsyncIterator, Iterator | ||
from typing import AsyncIterator | ||
|
||
import pytest | ||
|
||
from magentic import AsyncStreamedStr, StreamedStr | ||
from magentic.streamed_str import async_iter | ||
|
||
|
||
def test_streamed_str_iter(): | ||
def generator() -> Iterator[str]: | ||
yield from ["Hello", " World"] | ||
@pytest.mark.asyncio | ||
async def test_async_iter(): | ||
output = async_iter(["Hello", " World"]) | ||
assert isinstance(output, AsyncIterator) | ||
assert [chunk async for chunk in output] == ["Hello", " World"] | ||
|
||
streamed_str = StreamedStr(generator()) | ||
|
||
def test_streamed_str_iter(): | ||
iter_chunks = iter(["Hello", " World"]) | ||
streamed_str = StreamedStr(iter_chunks) | ||
assert list(streamed_str) == ["Hello", " World"] | ||
assert list(iter_chunks) == [] # iterator is exhausted | ||
assert list(streamed_str) == ["Hello", " World"] | ||
|
||
|
||
def test_streamed_str_str(): | ||
def generator() -> Iterator[str]: | ||
yield from ["Hello", " World"] | ||
|
||
streamed_str = StreamedStr(generator()) | ||
streamed_str = StreamedStr(["Hello", " World"]) | ||
assert str(streamed_str) == "Hello World" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_streamed_str_iter(): | ||
async def generator() -> AsyncIterator[str]: | ||
for chunk in ["Hello", " World"]: | ||
yield chunk | ||
|
||
async_streamed_str = AsyncStreamedStr(generator()) | ||
aiter_chunks = async_iter(["Hello", " World"]) | ||
async_streamed_str = AsyncStreamedStr(aiter_chunks) | ||
assert [chunk async for chunk in async_streamed_str] == ["Hello", " World"] | ||
assert [chunk async for chunk in aiter_chunks] == [] # iterator is exhausted | ||
assert [chunk async for chunk in async_streamed_str] == ["Hello", " World"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_streamed_str_to_string(): | ||
async def generator() -> AsyncIterator[str]: | ||
for chunk in ["Hello", " World"]: | ||
yield chunk | ||
|
||
async_streamed_str = AsyncStreamedStr(generator()) | ||
async_streamed_str = AsyncStreamedStr(async_iter(["Hello", " World"])) | ||
assert await async_streamed_str.to_string() == "Hello World" |