Skip to content

Commit ab75b69

Browse files
Make collections.abcs more consistent with runtime implementation (#10816)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent d2ca658 commit ab75b69

File tree

5 files changed

+25
-67
lines changed

5 files changed

+25
-67
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
# Please keep sorted alphabetically
66

7-
_collections_abc.AsyncGenerator.ag_await
8-
_collections_abc.AsyncGenerator.ag_code
9-
_collections_abc.AsyncGenerator.ag_frame
10-
_collections_abc.AsyncGenerator.ag_running
11-
asyncio.__all__
127
builtins.dict.get
138
collections\.ChainMap\.fromkeys # https://github.com/python/mypy/issues/17023
149
http.client.HTTPConnection.response_class # the actual type at runtime is abc.ABCMeta
@@ -193,16 +188,6 @@ _collections_abc.AsyncIterator.__anext__
193188
_collections_abc.ByteString
194189

195190
_collections_abc.Callable # Typing-related weirdness
196-
197-
# Coroutine and Generator properties are added programmatically
198-
_collections_abc.Coroutine.cr_await
199-
_collections_abc.Coroutine.cr_code
200-
_collections_abc.Coroutine.cr_frame
201-
_collections_abc.Coroutine.cr_running
202-
_collections_abc.Generator.gi_code
203-
_collections_abc.Generator.gi_frame
204-
_collections_abc.Generator.gi_running
205-
_collections_abc.Generator.gi_yieldfrom
206191
_collections_abc.Mapping.get # Adding None to the Union messed up mypy
207192
_collections_abc.Sequence.index # Supporting None in end is not mandatory
208193

stdlib/@tests/stubtest_allowlists/py38.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,10 @@ tkinter.tix.wantobjects
5959
builtins.input # Incorrect default value in text signature, fixed in 3.10
6060
collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
6161
collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
62-
collections.AsyncGenerator.ag_await
63-
collections.AsyncGenerator.ag_code
64-
collections.AsyncGenerator.ag_frame
65-
collections.AsyncGenerator.ag_running
6662
collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
6763
collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
6864
collections.ByteString # see comments in py3_common.txt
6965
collections.Callable
70-
collections.Coroutine.cr_await
71-
collections.Coroutine.cr_code
72-
collections.Coroutine.cr_frame
73-
collections.Coroutine.cr_running
74-
collections.Generator.gi_code
75-
collections.Generator.gi_frame
76-
collections.Generator.gi_running
77-
collections.Generator.gi_yieldfrom
7866
collections.Mapping.get # Adding None to the Union messed up mypy
7967
collections.Sequence.index # Supporting None in end is not mandatory
8068
xxsubtype # module missing from the stubs

stdlib/@tests/stubtest_allowlists/py39.txt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,10 @@ typing._SpecialForm.__mro_entries__ # Exists at runtime, but missing from stubs
5050
builtins.input # Incorrect default value in text signature, fixed in 3.10
5151
collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491
5252
collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491
53-
collections.AsyncGenerator.ag_await
54-
collections.AsyncGenerator.ag_code
55-
collections.AsyncGenerator.ag_frame
56-
collections.AsyncGenerator.ag_running
5753
collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also.
5854
collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491
5955
collections.ByteString # see comments in py3_common.txt
6056
collections.Callable
61-
collections.Coroutine.cr_await
62-
collections.Coroutine.cr_code
63-
collections.Coroutine.cr_frame
64-
collections.Coroutine.cr_running
65-
collections.Generator.gi_code
66-
collections.Generator.gi_frame
67-
collections.Generator.gi_running
68-
collections.Generator.gi_yieldfrom
6957
collections.Mapping.get # Adding None to the Union messed up mypy
7058
collections.Sequence.index # Supporting None in end is not mandatory
7159
xxsubtype # module missing from the stubs
@@ -141,7 +129,6 @@ tkinter.tix.Shell
141129
tkinter.tix.TclVersion
142130
tkinter.tix.TkVersion
143131

144-
145132
# ============================================================
146133
# Allowlist entries that cannot or should not be fixed; >= 3.9
147134
# ============================================================

stdlib/types.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ _ReturnT_co = TypeVar("_ReturnT_co", covariant=True)
374374

375375
@final
376376
class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]):
377+
@property
378+
def gi_code(self) -> CodeType: ...
379+
@property
380+
def gi_frame(self) -> FrameType: ...
381+
@property
382+
def gi_running(self) -> bool: ...
377383
@property
378384
def gi_yieldfrom(self) -> GeneratorType[_YieldT_co, _SendT_contra, Any] | None: ...
379385
if sys.version_info >= (3, 11):
@@ -397,6 +403,12 @@ class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]):
397403
class AsyncGeneratorType(AsyncGenerator[_YieldT_co, _SendT_contra]):
398404
@property
399405
def ag_await(self) -> Awaitable[Any] | None: ...
406+
@property
407+
def ag_code(self) -> CodeType: ...
408+
@property
409+
def ag_frame(self) -> FrameType: ...
410+
@property
411+
def ag_running(self) -> bool: ...
400412
__name__: str
401413
__qualname__: str
402414
if sys.version_info >= (3, 12):
@@ -421,6 +433,14 @@ class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]):
421433
__name__: str
422434
__qualname__: str
423435
@property
436+
def cr_await(self) -> Any | None: ...
437+
@property
438+
def cr_code(self) -> CodeType: ...
439+
@property
440+
def cr_frame(self) -> FrameType: ...
441+
@property
442+
def cr_running(self) -> bool: ...
443+
@property
424444
def cr_origin(self) -> tuple[tuple[str, int, str], ...] | None: ...
425445
if sys.version_info >= (3, 11):
426446
@property

stdlib/typing.pyi

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ from re import Match as Match, Pattern as Pattern
1212
from types import (
1313
BuiltinFunctionType,
1414
CodeType,
15-
FrameType,
1615
FunctionType,
1716
MethodDescriptorType,
1817
MethodType,
@@ -473,7 +472,8 @@ _YieldT_co = TypeVar("_YieldT_co", covariant=True)
473472
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None)
474473
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None)
475474

476-
class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _ReturnT_co]):
475+
@runtime_checkable
476+
class Generator(Iterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra, _ReturnT_co]):
477477
def __next__(self) -> _YieldT_co: ...
478478
@abstractmethod
479479
def send(self, value: _SendT_contra, /) -> _YieldT_co: ...
@@ -491,14 +491,6 @@ class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _Return
491491
def close(self) -> None: ...
492492

493493
def __iter__(self) -> Generator[_YieldT_co, _SendT_contra, _ReturnT_co]: ...
494-
@property
495-
def gi_code(self) -> CodeType: ...
496-
@property
497-
def gi_frame(self) -> FrameType: ...
498-
@property
499-
def gi_running(self) -> bool: ...
500-
@property
501-
def gi_yieldfrom(self) -> Generator[Any, Any, Any] | None: ...
502494

503495
# NOTE: Prior to Python 3.13 these aliases are lacking the second _ExitT_co parameter
504496
if sys.version_info >= (3, 13):
@@ -524,14 +516,7 @@ _ReturnT_co_nd = TypeVar("_ReturnT_co_nd", covariant=True)
524516
class Coroutine(Awaitable[_ReturnT_co_nd], Generic[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd]):
525517
__name__: str
526518
__qualname__: str
527-
@property
528-
def cr_await(self) -> Any | None: ...
529-
@property
530-
def cr_code(self) -> CodeType: ...
531-
@property
532-
def cr_frame(self) -> FrameType | None: ...
533-
@property
534-
def cr_running(self) -> bool: ...
519+
535520
@abstractmethod
536521
def send(self, value: _SendT_contra_nd, /) -> _YieldT_co: ...
537522
@overload
@@ -566,7 +551,8 @@ class AsyncIterator(AsyncIterable[_T_co], Protocol[_T_co]):
566551
def __anext__(self) -> Awaitable[_T_co]: ...
567552
def __aiter__(self) -> AsyncIterator[_T_co]: ...
568553

569-
class AsyncGenerator(AsyncIterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra]):
554+
@runtime_checkable
555+
class AsyncGenerator(AsyncIterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra]):
570556
def __anext__(self) -> Coroutine[Any, Any, _YieldT_co]: ...
571557
@abstractmethod
572558
def asend(self, value: _SendT_contra, /) -> Coroutine[Any, Any, _YieldT_co]: ...
@@ -581,14 +567,6 @@ class AsyncGenerator(AsyncIterator[_YieldT_co], Generic[_YieldT_co, _SendT_contr
581567
self, typ: BaseException, val: None = None, tb: TracebackType | None = None, /
582568
) -> Coroutine[Any, Any, _YieldT_co]: ...
583569
def aclose(self) -> Coroutine[Any, Any, None]: ...
584-
@property
585-
def ag_await(self) -> Any: ...
586-
@property
587-
def ag_code(self) -> CodeType: ...
588-
@property
589-
def ag_frame(self) -> FrameType: ...
590-
@property
591-
def ag_running(self) -> bool: ...
592570

593571
@runtime_checkable
594572
class Container(Protocol[_T_co]):

0 commit comments

Comments
 (0)