Skip to content

Commit

Permalink
Fix itertools typing stubs (#140)
Browse files Browse the repository at this point in the history
* Fix itertools typing stubs
  • Loading branch information
isra17 authored Apr 11, 2024
1 parent 04e0925 commit aaeb6d7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion asyncstdlib/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __lt__(self: LT, other: LT) -> bool:


class SupportsAdd(Protocol):
def __add__(self: ADD, other: ADD) -> ADD:
def __add__(self: ADD, other: ADD, /) -> ADD:
raise NotImplementedError


Expand Down
22 changes: 10 additions & 12 deletions asyncstdlib/itertools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,51 @@ class chain(AsyncIterator[T]):
async def aclose(self) -> None: ...

def compress(data: AnyIterable[T], selectors: AnyIterable[Any]) -> AsyncIterator[T]: ...
async def dropwhile(
def dropwhile(
predicate: Callable[[T], Any], iterable: AnyIterable[T]
) -> AsyncIterator[T]: ...
async def filterfalse(
def filterfalse(
predicate: Callable[[T], Any] | None, iterable: AnyIterable[T]
) -> AsyncIterator[T]: ...
@overload
async def islice(
iterable: AnyIterable[T], start: int | None, /
) -> AsyncIterator[T]: ...
def islice(iterable: AnyIterable[T], start: int | None, /) -> AsyncIterator[T]: ...
@overload
async def islice(
def islice(
iterable: AnyIterable[T],
start: int | None,
stop: int | None,
step: int | None = None,
/,
) -> AsyncIterator[T]: ...
@overload
async def starmap(
def starmap(
function: Callable[[T1], T] | Callable[[T1], Awaitable[T]],
iterable: AnyIterable[tuple[T1]],
) -> AsyncIterator[T]: ...
@overload
async def starmap(
def starmap(
function: Callable[[T1, T2], T] | Callable[[T1, T2], Awaitable[T]],
iterable: AnyIterable[tuple[T1, T2]],
) -> AsyncIterator[T]: ...
@overload
async def starmap(
def starmap(
function: Callable[[T1, T2, T3], T] | Callable[[T1, T2, T3], Awaitable[T]],
iterable: AnyIterable[tuple[T1, T2, T3]],
) -> AsyncIterator[T]: ...
@overload
async def starmap(
def starmap(
function: Callable[[T1, T2, T3, T4], T] | Callable[[T1, T2, T3, T4], Awaitable[T]],
iterable: AnyIterable[tuple[T1, T2, T3, T4]],
) -> AsyncIterator[T]: ...
@overload
async def starmap(
def starmap(
function: (
Callable[[T1, T2, T3, T4, T5], T] | Callable[[T1, T2, T3, T4, T5], Awaitable[T]]
),
iterable: AnyIterable[tuple[T1, T2, T3, T4, T5]],
) -> AsyncIterator[T]: ...
@overload
async def starmap(
def starmap(
function: Callable[..., T] | Callable[..., Awaitable[T]],
iterable: AnyIterable[Iterable[Any]],
) -> AsyncIterator[T]: ...
Expand Down
71 changes: 71 additions & 0 deletions typetests/test_itertools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import AsyncIterator
from asyncstdlib import itertools
from typing_extensions import assert_type


async def test_cycle() -> None:
async for x in itertools.cycle([1]):
assert_type(x, int)


async def test_accumulate() -> None:
async for x in itertools.accumulate([1]):
assert_type(x, int)


async def test_batched() -> None:
async for x in itertools.batched([1], 1):
assert_type(x, "tuple[int]")


async def test_chain() -> None:
async for x in itertools.chain([1]):
assert_type(x, int)


async def test_compress() -> None:
async for x in itertools.compress([1], [1]):
assert_type(x, int)


async def test_dropwhile() -> None:
async for x in itertools.dropwhile(lambda x: True, [1]):
assert_type(x, int)


async def test_filterfalse() -> None:
async for x in itertools.filterfalse(lambda x: True, [1]):
assert_type(x, int)


async def test_starmap() -> None:
def f(x: str) -> int:
return int(x)

async for x in itertools.starmap(f, [("1",)]):
assert_type(x, int)


async def test_takewhile() -> None:
async for x in itertools.takewhile(lambda x: True, [1]):
assert_type(x, int)


async def test_tee() -> None:
async for x in itertools.tee([1])[0]:
assert_type(x, int)


async def test_pairwise() -> None:
async for x in itertools.pairwise([1]):
assert_type(x, "tuple[int, int]")


async def test_zip_longest() -> None:
async for x in itertools.zip_longest([1]):
assert_type(x, "tuple[int]")


async def test_groupby() -> None:
async for x in itertools.groupby([1]):
assert_type(x, "tuple[int, AsyncIterator[int]]")

0 comments on commit aaeb6d7

Please sign in to comment.