Files
wehub-resource-sync 9201ef759e
Harness Compat / harness compat (push) Failing after 0s
CI / test on 3.12 (standard) (push) Has been cancelled
CI / test on 3.13 (standard) (push) Has been cancelled
CI / test on 3.14 (standard) (push) Has been cancelled
CI / test on 3.10 (all-extras) (push) Has been cancelled
CI / test on 3.11 (all-extras) (push) Has been cancelled
CI / test on 3.12 (all-extras) (push) Has been cancelled
CI / test on 3.14 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.10 (pydantic-evals) (push) Has been cancelled
CI / test on 3.11 (pydantic-evals) (push) Has been cancelled
CI / test on 3.12 (pydantic-evals) (push) Has been cancelled
CI / deploy-docs-preview (push) Has been cancelled
CI / build release artifacts (push) Has been cancelled
CI / publish to PyPI (push) Has been cancelled
CI / Send tweet (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / mypy (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / test on 3.10 (standard) (push) Has been cancelled
CI / test on 3.11 (standard) (push) Has been cancelled
CI / test on 3.13 (all-extras) (push) Has been cancelled
CI / test on 3.14 (all-extras) (push) Has been cancelled
CI / test on 3.10 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.11 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.12 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-evals) (push) Has been cancelled
CI / test on 3.14 (pydantic-evals) (push) Has been cancelled
CI / test on 3.10 (lowest-versions) (push) Has been cancelled
CI / test on 3.11 (lowest-versions) (push) Has been cancelled
CI / test on 3.12 (lowest-versions) (push) Has been cancelled
CI / test on 3.13 (lowest-versions) (push) Has been cancelled
CI / test on 3.14 (lowest-versions) (push) Has been cancelled
CI / test examples on 3.11 (push) Has been cancelled
CI / test examples on 3.12 (push) Has been cancelled
CI / test examples on 3.13 (push) Has been cancelled
CI / test examples on 3.14 (push) Has been cancelled
CI / coverage (push) Has been cancelled
CI / check (push) Has been cancelled
CI / deploy-docs (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:27:52 +08:00

77 lines
2.1 KiB
Python

"""
This module provides an asynchronous context manager and iterator,
`MockAsyncStream`, which wraps a synchronous iterator and exposes it
as an asynchronous stream. It can be used in async/await code blocks
and async for-loops, emulating asynchronous iteration behavior.
"""
from __future__ import annotations as _annotations
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any, Generic, TypeVar
from pydantic_ai import _utils
from ..conftest import raise_if_exception
T = TypeVar('T')
@dataclass
class MockAsyncStream(Generic[T]):
"""Wraps a synchronous iterator in an asynchronous interface.
This class allows a synchronous iterator to be treated as an
asynchronous iterator, enabling iteration in `async for` loops
and usage within `async with` blocks.
Example usage:
async def example():
sync_iter = iter([1, 2, 3])
async_stream = MockAsyncStream(sync_iter)
async for item in async_stream:
print(item)
async with MockAsyncStream(sync_iter) as stream:
async for item in stream:
print(item)
"""
_iter: Iterator[T]
"""The underlying synchronous iterator."""
async def __anext__(self) -> T:
"""Return the next item from the synchronous iterator as if it were asynchronous.
Calls `_utils.sync_anext` to retrieve the next item from the underlying
synchronous iterator. If the iterator is exhausted, `StopAsyncIteration`
is raised.
"""
next = _utils.sync_anext(self._iter)
raise_if_exception(next)
return next
def __aiter__(self) -> MockAsyncStream[T]:
return self
async def __aenter__(self) -> MockAsyncStream[T]:
return self
async def __aexit__(self, *_args: Any) -> None:
pass
async def close(self) -> None:
pass
async def aclose(self) -> None:
pass
def cancel(self) -> None:
pass
@property
def response(self) -> MockAsyncStream[T]:
return self