Skip to content

Commit 5c44ae4

Browse files
Improve various signatures that shouldn't be async def, but currently are (#7491)
Co-authored-by: Thomas Grainger <[email protected]>
1 parent 37a9819 commit 5c44ae4

File tree

10 files changed

+44
-17
lines changed

10 files changed

+44
-17
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ctypes
77
import mmap
88
import sys
99
from os import PathLike
10-
from typing import AbstractSet, Any, Container, Generic, Iterable, Protocol, TypeVar
10+
from typing import AbstractSet, Any, Awaitable, Container, Generic, Iterable, Protocol, TypeVar
1111
from typing_extensions import Final, Literal, final
1212

1313
_KT = TypeVar("_KT")
@@ -33,7 +33,7 @@ class SupportsNext(Protocol[_T_co]):
3333

3434
# stable
3535
class SupportsAnext(Protocol[_T_co]):
36-
async def __anext__(self) -> _T_co: ...
36+
def __anext__(self) -> Awaitable[_T_co]: ...
3737

3838
# Comparison protocols
3939

stdlib/builtins.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from typing import (
2929
IO,
3030
AbstractSet,
3131
Any,
32+
Awaitable,
3233
BinaryIO,
3334
ByteString,
3435
Generic,
@@ -72,6 +73,8 @@ _T4 = TypeVar("_T4")
7273
_T5 = TypeVar("_T5")
7374
_SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=True)
7475
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
76+
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
77+
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)
7578

7679
class _SupportsIter(Protocol[_T_co]):
7780
def __iter__(self) -> _T_co: ...
@@ -1068,8 +1071,15 @@ class _PathLike(Protocol[_AnyStr_co]):
10681071

10691072
if sys.version_info >= (3, 10):
10701073
def aiter(__async_iterable: _SupportsAiter[_SupportsAnextT]) -> _SupportsAnextT: ...
1074+
1075+
class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
1076+
def __anext__(self) -> _AwaitableT_co: ...
1077+
10711078
@overload
1072-
async def anext(__i: SupportsAnext[_T]) -> _T: ...
1079+
# `anext` is not, in fact, an async function. When default is not provided
1080+
# `anext` is just a passthrough for `obj.__anext__`
1081+
# See discussion in #7491 and pure-Python implementation of `anext` at https://github.com/python/cpython/blob/ea786a882b9ed4261eafabad6011bc7ef3b5bf94/Lib/test/test_asyncgen.py#L52-L80
1082+
def anext(__i: _SupportsSynchronousAnext[_AwaitableT]) -> _AwaitableT: ...
10731083
@overload
10741084
async def anext(__i: SupportsAnext[_T], default: _VT) -> _T | _VT: ...
10751085

stdlib/contextlib.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class closing(AbstractContextManager[_SupportsCloseT]):
154154

155155
if sys.version_info >= (3, 10):
156156
class _SupportsAclose(Protocol):
157-
async def aclose(self) -> object: ...
157+
def aclose(self) -> Awaitable[object]: ...
158158
_SupportsAcloseT = TypeVar("_SupportsAcloseT", bound=_SupportsAclose)
159159

160160
class aclosing(AbstractAsyncContextManager[_SupportsAcloseT]):

stdlib/typing.pyi

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -752,26 +752,22 @@ class AsyncIterable(Protocol[_T_co]):
752752
@runtime_checkable
753753
class AsyncIterator(AsyncIterable[_T_co], Protocol[_T_co]):
754754
@abstractmethod
755-
async def __anext__(self) -> _T_co: ...
755+
def __anext__(self) -> Awaitable[_T_co]: ...
756756
def __aiter__(self) -> AsyncIterator[_T_co]: ...
757757

758758
class AsyncGenerator(AsyncIterator[_T_co], Generic[_T_co, _T_contra]):
759+
def __anext__(self) -> Awaitable[_T_co]: ...
759760
@abstractmethod
760-
async def __anext__(self) -> _T_co: ...
761-
@abstractmethod
762-
async def asend(self, __value: _T_contra) -> _T_co: ...
761+
def asend(self, __value: _T_contra) -> Awaitable[_T_co]: ...
763762
@overload
764763
@abstractmethod
765-
async def athrow(
764+
def athrow(
766765
self, __typ: Type[BaseException], __val: BaseException | object = ..., __tb: TracebackType | None = ...
767-
) -> _T_co: ...
766+
) -> Awaitable[_T_co]: ...
768767
@overload
769768
@abstractmethod
770-
async def athrow(self, __typ: BaseException, __val: None = ..., __tb: TracebackType | None = ...) -> _T_co: ...
771-
@abstractmethod
772-
async def aclose(self) -> None: ...
773-
@abstractmethod
774-
def __aiter__(self) -> AsyncGenerator[_T_co, _T_contra]: ...
769+
def athrow(self, __typ: BaseException, __val: None = ..., __tb: TracebackType | None = ...) -> Awaitable[_T_co]: ...
770+
def aclose(self) -> Awaitable[None]: ...
775771
@property
776772
def ag_await(self) -> Any: ...
777773
@property

tests/stubtest_allowlists/py310.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typing.IO.truncate
2323
typing.IO.write
2424
typing.IO.writelines
2525

26+
_collections_abc.AsyncGenerator.athrow # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
2627
_weakref.ProxyType.__reversed__ # Doesn't really exist
2728
ast.Bytes.__new__
2829
ast.Ellipsis.__new__
@@ -106,8 +107,6 @@ xml.etree.ElementTree.XMLParser.__init__ # Defined in C so has general signatur
106107
xml.etree.cElementTree.XMLParser.__init__ # Defined in C so has general signature
107108

108109
# positional-only complaints caused by differences between typing aliases and the "real" classes in the stdlib
109-
_collections_abc.AsyncGenerator.asend
110-
_collections_abc.AsyncGenerator.athrow
111110
_collections_abc.Coroutine.send
112111
_collections_abc.Coroutine.throw
113112
_collections_abc.Generator.send

tests/stubtest_allowlists/py36.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ asyncio.locks._ContextManagerMixin.__exit__ # Always raises; deliberately omitt
1111
builtins.float.__setformat__ # Internal method for CPython test suite
1212
builtins.str.maketrans
1313
cmath.log
14+
collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
15+
collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
16+
collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
17+
collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
1418
collections.AsyncGenerator.ag_await
1519
collections.AsyncGenerator.ag_code
1620
collections.AsyncGenerator.ag_frame
@@ -89,6 +93,8 @@ tkinter.filedialog.TkVersion
8993
tkinter.filedialog.wantobjects
9094
tkinter.simpledialog.wantobjects
9195
tkinter.tix.wantobjects
96+
typing.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
97+
typing.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491
9298

9399
builtins.memoryview.__iter__ # C type that implements __getitem__
94100
builtins.memoryview.cast # inspect.signature is incorrect about shape being kw-only

tests/stubtest_allowlists/py37.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ builtins.dict.get
1515
builtins.float.__set_format__ # Internal method for CPython test suite
1616
builtins.str.maketrans
1717
cmath.log
18+
collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
19+
collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
20+
collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
21+
collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
1822
collections.AsyncGenerator.ag_await
1923
collections.AsyncGenerator.ag_code
2024
collections.AsyncGenerator.ag_frame

tests/stubtest_allowlists/py38.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ asyncio.locks._ContextManagerMixin.__enter__ # Always raises; deliberately omit
1919
asyncio.locks._ContextManagerMixin.__exit__ # Always raises; deliberately omitted from the stub
2020
builtins.dict.get
2121
builtins.float.__set_format__ # Internal method for CPython test suite
22+
collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
23+
collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
24+
collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
25+
collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
2226
collections.AsyncGenerator.ag_await
2327
collections.AsyncGenerator.ag_code
2428
collections.AsyncGenerator.ag_frame

tests/stubtest_allowlists/py39.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ asyncio.futures.Future.__init__ # Usually initialized from c object
2121
asyncio.futures.Future._callbacks # Usually initialized from c object
2222
builtins.dict.get
2323
builtins.float.__set_format__ # Internal method for CPython test suite
24+
collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
25+
collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
26+
collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
27+
collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
2428
collections.AsyncGenerator.ag_await
2529
collections.AsyncGenerator.ag_code
2630
collections.AsyncGenerator.ag_frame

tests/stubtest_allowlists/py3_common.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ xml.parsers.expat.expat_CAPI
246246
# ==========
247247
# Allowlist entries that cannot or should not be fixed
248248
# ==========
249+
_collections_abc.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
250+
_collections_abc.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
251+
_collections_abc.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
252+
_collections_abc.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
249253
_pydecimal.* # See comments in file
250254
_weakref.ProxyType.__bytes__ # Doesn't really exist
251255
ast.NodeVisitor.visit_\w+ # Methods are discovered dynamically, see #3796

0 commit comments

Comments
 (0)