HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/aigenerator/venv/lib/python3.12/site-packages/stripe/_any_iterator.py
from typing import TypeVar, Iterator, AsyncIterator

T = TypeVar("T")


class AnyIterator(Iterator[T], AsyncIterator[T]):
    """
    AnyIterator supports iteration through both `for ... in <AnyIterator>` and `async for ... in <AnyIterator> syntaxes.
    """

    def __init__(
        self, iterator: Iterator[T], async_iterator: AsyncIterator[T]
    ) -> None:
        self._iterator = iterator
        self._async_iterator = async_iterator

        self._sync_iterated = False
        self._async_iterated = False

    def __next__(self) -> T:
        if self._async_iterated:
            raise RuntimeError(
                "AnyIterator error: cannot mix sync and async iteration"
            )
        self._sync_iterated = True
        return self._iterator.__next__()

    async def __anext__(self) -> T:
        if self._sync_iterated:
            raise RuntimeError(
                "AnyIterator error: cannot mix sync and async iteration"
            )
        self._async_iterated = True
        return await self._async_iterator.__anext__()