Skip to content

Commit

Permalink
Merge pull request #152 from maxfischer2781/maintenance/static_checks…
Browse files Browse the repository at this point in the history
…_607

Type fixes
  • Loading branch information
maxfischer2781 authored Aug 4, 2024
2 parents e08a044 + c6c1410 commit ce2d0d4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
6 changes: 4 additions & 2 deletions asyncstdlib/_lrucache.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def __get__(
"""Descriptor ``__get__`` for caches to bind them on lookup"""
if instance is None:
return self
return LRUAsyncBoundCallable(self, instance)
return LRUAsyncBoundCallable(
self, instance
) # pyright: ignore[reportUnknownVariableType]

#: Get the result of ``await __wrapped__(...)`` from the cache or evaluation
__call__: AC
Expand Down Expand Up @@ -120,7 +122,7 @@ def cache_discard(self, *args: Any, **kwargs: Any) -> None:
# these are fake and only exist for placeholders
S = TypeVar("S")
S2 = TypeVar("S2")
P = TypeVar("P")
P = TypeVar("P") # actually a ParamSpec, see .pyi
R = TypeVar("R")


Expand Down
14 changes: 12 additions & 2 deletions asyncstdlib/asynctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,17 @@ def __repr__(self) -> str:
return f"<{self.__class__.__name__} of {self._iterator!r} at 0x{(id(self)):x}>"


def borrow(iterator: AsyncIterator[T], /) -> AsyncIterator[T]:
@overload
def borrow(iterator: AsyncGenerator[T, S], /) -> AsyncGenerator[T, S]: ...


@overload
def borrow(iterator: AsyncIterator[T], /) -> AsyncIterator[T]: ...


def borrow(
iterator: Union[AsyncIterator[T], AsyncGenerator[T, Any]], /
) -> Union[AsyncIterator[T], AsyncGenerator[T, Any]]:
"""
Borrow an async iterator, preventing to ``aclose`` it
Expand All @@ -142,7 +152,7 @@ def borrow(iterator: AsyncIterator[T], /) -> AsyncIterator[T]:
"borrowing requires an async iterator "
+ f"with __aiter__ and __anext__ method, got {type(iterator).__name__}"
)
return _BorrowedAsyncIterator(iterator)
return _BorrowedAsyncIterator[T, Any](iterator)


def scoped_iter(iterable: AnyIterable[T], /) -> AsyncContextManager[AsyncIterator[T]]:
Expand Down
2 changes: 1 addition & 1 deletion asyncstdlib/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def __getitem__(
) -> Union[AsyncIterator[T], Tuple[AsyncIterator[T], ...]]:
return self._children[item]

def __iter__(self) -> Iterator[AnyIterable[T]]:
def __iter__(self) -> Iterator[AsyncIterator[T]]:
yield from self._children

async def __aenter__(self) -> "Tee[T]":
Expand Down
2 changes: 1 addition & 1 deletion asyncstdlib/itertools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class tee(Generic[T]):
def __getitem__(self, item: int) -> AsyncIterator[T]: ...
@overload
def __getitem__(self, item: slice) -> tuple[AsyncIterator[T], ...]: ...
def __iter__(self) -> Iterator[AnyIterable[T]]: ...
def __iter__(self) -> Iterator[AsyncIterator[T]]: ...
async def __aenter__(self: Self) -> Self: ...
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ...
async def aclose(self) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
statistics = True
max-line-length = 80
ignore = E302, E501, E704, B008, B011, B905, B950, W503
ignore = E302, E501, E704, B008, B011, B905, B950, W503, W504
select = C,E,F,W,B,B9
exclude = docs,.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg
10 changes: 10 additions & 0 deletions typetests/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ async def test_tee() -> None:
assert_type(x, int)


async def test_tee_iter() -> None:
x1, x2 = itertools.tee([1], n=2)
assert_type(x1, AsyncIterator[int])
assert_type(x2, AsyncIterator[int])

for xi in itertools.tee([1], n=2):
async for x in xi:
assert_type(x, int)


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

0 comments on commit ce2d0d4

Please sign in to comment.