Skip to content

Commit ce2d0d4

Browse files
Merge pull request #152 from maxfischer2781/maintenance/static_checks_607
Type fixes
2 parents e08a044 + c6c1410 commit ce2d0d4

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

asyncstdlib/_lrucache.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def __get__(
8181
"""Descriptor ``__get__`` for caches to bind them on lookup"""
8282
if instance is None:
8383
return self
84-
return LRUAsyncBoundCallable(self, instance)
84+
return LRUAsyncBoundCallable(
85+
self, instance
86+
) # pyright: ignore[reportUnknownVariableType]
8587

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

126128

asyncstdlib/asynctools.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ def __repr__(self) -> str:
119119
return f"<{self.__class__.__name__} of {self._iterator!r} at 0x{(id(self)):x}>"
120120

121121

122-
def borrow(iterator: AsyncIterator[T], /) -> AsyncIterator[T]:
122+
@overload
123+
def borrow(iterator: AsyncGenerator[T, S], /) -> AsyncGenerator[T, S]: ...
124+
125+
126+
@overload
127+
def borrow(iterator: AsyncIterator[T], /) -> AsyncIterator[T]: ...
128+
129+
130+
def borrow(
131+
iterator: Union[AsyncIterator[T], AsyncGenerator[T, Any]], /
132+
) -> Union[AsyncIterator[T], AsyncGenerator[T, Any]]:
123133
"""
124134
Borrow an async iterator, preventing to ``aclose`` it
125135
@@ -142,7 +152,7 @@ def borrow(iterator: AsyncIterator[T], /) -> AsyncIterator[T]:
142152
"borrowing requires an async iterator "
143153
+ f"with __aiter__ and __anext__ method, got {type(iterator).__name__}"
144154
)
145-
return _BorrowedAsyncIterator(iterator)
155+
return _BorrowedAsyncIterator[T, Any](iterator)
146156

147157

148158
def scoped_iter(iterable: AnyIterable[T], /) -> AsyncContextManager[AsyncIterator[T]]:

asyncstdlib/itertools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def __getitem__(
454454
) -> Union[AsyncIterator[T], Tuple[AsyncIterator[T], ...]]:
455455
return self._children[item]
456456

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

460460
async def __aenter__(self) -> "Tee[T]":

asyncstdlib/itertools.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class tee(Generic[T]):
130130
def __getitem__(self, item: int) -> AsyncIterator[T]: ...
131131
@overload
132132
def __getitem__(self, item: slice) -> tuple[AsyncIterator[T], ...]: ...
133-
def __iter__(self) -> Iterator[AnyIterable[T]]: ...
133+
def __iter__(self) -> Iterator[AsyncIterator[T]]: ...
134134
async def __aenter__(self: Self) -> Self: ...
135135
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ...
136136
async def aclose(self) -> None: ...

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
statistics = True
33
max-line-length = 80
4-
ignore = E302, E501, E704, B008, B011, B905, B950, W503
4+
ignore = E302, E501, E704, B008, B011, B905, B950, W503, W504
55
select = C,E,F,W,B,B9
66
exclude = docs,.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg

typetests/test_itertools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ async def test_tee() -> None:
5656
assert_type(x, int)
5757

5858

59+
async def test_tee_iter() -> None:
60+
x1, x2 = itertools.tee([1], n=2)
61+
assert_type(x1, AsyncIterator[int])
62+
assert_type(x2, AsyncIterator[int])
63+
64+
for xi in itertools.tee([1], n=2):
65+
async for x in xi:
66+
assert_type(x, int)
67+
68+
5969
async def test_pairwise() -> None:
6070
async for x in itertools.pairwise([1]):
6171
assert_type(x, "tuple[int, int]")

0 commit comments

Comments
 (0)