Skip to content

Commit 33ecb68

Browse files
authored
Fix return annotations of several methods that return self at runtime (#7070)
1 parent 749d3db commit 33ecb68

File tree

8 files changed

+19
-15
lines changed

8 files changed

+19
-15
lines changed

stdlib/asyncio/streams.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import StrPath
2+
from _typeshed import Self, StrPath
33
from typing import Any, AsyncIterator, Awaitable, Callable, Iterable, Optional
44

55
from . import events, protocols, transports
@@ -117,7 +117,7 @@ class StreamWriter:
117117
def get_extra_info(self, name: str, default: Any = ...) -> Any: ...
118118
async def drain(self) -> None: ...
119119

120-
class StreamReader:
120+
class StreamReader(AsyncIterator[bytes]):
121121
def __init__(self, limit: int = ..., loop: events.AbstractEventLoop | None = ...) -> None: ...
122122
def exception(self) -> Exception: ...
123123
def set_exception(self, exc: Exception) -> None: ...
@@ -129,5 +129,5 @@ class StreamReader:
129129
async def readuntil(self, separator: bytes = ...) -> bytes: ...
130130
async def read(self, n: int = ...) -> bytes: ...
131131
async def readexactly(self, n: int) -> bytes: ...
132-
def __aiter__(self) -> AsyncIterator[bytes]: ...
132+
def __aiter__(self: Self) -> Self: ...
133133
async def __anext__(self) -> bytes: ...

stdlib/codecs.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class CodecInfo(tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
8686
def incrementaldecoder(self) -> _IncrementalDecoder: ...
8787
name: str
8888
def __new__(
89-
cls,
89+
cls: type[Self],
9090
encode: _Encoder,
9191
decode: _Decoder,
9292
streamreader: _StreamReader | None = ...,
@@ -96,7 +96,7 @@ class CodecInfo(tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
9696
name: str | None = ...,
9797
*,
9898
_is_text_encoding: bool | None = ...,
99-
) -> CodecInfo: ...
99+
) -> Self: ...
100100

101101
def getencoder(encoding: str) -> _Encoder: ...
102102
def getdecoder(encoding: str) -> _Decoder: ...
@@ -189,7 +189,7 @@ class StreamWriter(Codec):
189189
def __exit__(self, typ: type[BaseException] | None, exc: BaseException | None, tb: types.TracebackType | None) -> None: ...
190190
def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ...
191191

192-
class StreamReader(Codec):
192+
class StreamReader(Codec, Iterator[str]):
193193
errors: str
194194
def __init__(self, stream: IO[bytes], errors: str = ...) -> None: ...
195195
def read(self, size: int = ..., chars: int = ..., firstline: bool = ...) -> str: ...
@@ -198,7 +198,8 @@ class StreamReader(Codec):
198198
def reset(self) -> None: ...
199199
def __enter__(self: Self) -> Self: ...
200200
def __exit__(self, typ: type[BaseException] | None, exc: BaseException | None, tb: types.TracebackType | None) -> None: ...
201-
def __iter__(self) -> Iterator[str]: ...
201+
def __iter__(self: Self) -> Self: ...
202+
def __next__(self) -> str: ...
202203
def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ...
203204

204205
# Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing

stdlib/csv.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ from _csv import (
1717
unregister_dialect as unregister_dialect,
1818
writer as writer,
1919
)
20+
from _typeshed import Self
2021
from collections.abc import Collection, Iterable, Iterator, Mapping, Sequence
2122
from typing import Any, Generic, TypeVar, overload
2223

@@ -75,7 +76,7 @@ class DictReader(Generic[_T], Iterator[_DictReadMapping[_T, str]]):
7576
*args: Any,
7677
**kwds: Any,
7778
) -> None: ...
78-
def __iter__(self) -> DictReader[_T]: ...
79+
def __iter__(self: Self) -> Self: ...
7980
def __next__(self) -> _DictReadMapping[_T, str]: ...
8081

8182
class DictWriter(Generic[_T]):

stdlib/fileinput.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def fileno() -> int: ...
4646
def isfirstline() -> bool: ...
4747
def isstdin() -> bool: ...
4848

49-
class FileInput(Iterable[AnyStr], Generic[AnyStr]):
49+
class FileInput(Iterator[AnyStr], Generic[AnyStr]):
5050
if sys.version_info >= (3, 10):
5151
def __init__(
5252
self,
@@ -83,7 +83,7 @@ class FileInput(Iterable[AnyStr], Generic[AnyStr]):
8383
def close(self) -> None: ...
8484
def __enter__(self: Self) -> Self: ...
8585
def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ...
86-
def __iter__(self) -> Iterator[AnyStr]: ...
86+
def __iter__(self: Self) -> Self: ...
8787
def __next__(self) -> AnyStr: ...
8888
if sys.version_info < (3, 11):
8989
def __getitem__(self, i: int) -> AnyStr: ...

stdlib/pdb.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import signal
22
import sys
3+
from _typeshed import Self
34
from bdb import Bdb
45
from cmd import Cmd
56
from inspect import _SourceObjectType
@@ -173,4 +174,4 @@ def getsourcelines(obj: _SourceObjectType) -> tuple[list[str], int]: ...
173174
def lasti2lineno(code: CodeType, lasti: int) -> int: ...
174175

175176
class _rstr(str):
176-
def __repr__(self) -> _rstr: ...
177+
def __repr__(self: Self) -> Self: ...

stdlib/sre_constants.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import Self
23
from typing import Any
34

45
MAXGROUPS: int
@@ -15,7 +16,7 @@ class error(Exception):
1516

1617
class _NamedIntConstant(int):
1718
name: Any
18-
def __new__(cls, value: int, name: str) -> _NamedIntConstant: ...
19+
def __new__(cls: type[Self], value: int, name: str) -> Self: ...
1920

2021
MAXREPEAT: _NamedIntConstant
2122
OPCODES: list[_NamedIntConstant]

stdlib/ssl.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class SSLContext:
396396
if sys.version_info >= (3, 8):
397397
keylog_filename: str
398398
post_handshake_auth: bool
399-
def __new__(cls, protocol: int = ..., *args: Any, **kwargs: Any) -> SSLContext: ...
399+
def __new__(cls: type[Self], protocol: int = ..., *args: Any, **kwargs: Any) -> Self: ...
400400
def __init__(self, protocol: int = ...) -> None: ...
401401
def cert_store_stats(self) -> dict[str, int]: ...
402402
def load_cert_chain(

stdlib/weakref.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ _P = ParamSpec("_P")
2525
ProxyTypes: tuple[type[Any], ...]
2626

2727
class WeakMethod(ref[_CallableT], Generic[_CallableT]):
28-
def __new__(cls, meth: _CallableT, callback: Callable[[_CallableT], object] | None = ...) -> WeakMethod[_CallableT]: ...
28+
def __new__(cls: type[Self], meth: _CallableT, callback: Callable[[_CallableT], object] | None = ...) -> Self: ...
2929
def __call__(self) -> _CallableT | None: ...
3030

3131
class WeakValueDictionary(MutableMapping[_KT, _VT]):
@@ -67,7 +67,7 @@ class WeakValueDictionary(MutableMapping[_KT, _VT]):
6767
class KeyedRef(ref[_T], Generic[_KT, _T]):
6868
key: _KT
6969
# This __new__ method uses a non-standard name for the "cls" parameter
70-
def __new__(type, ob: _T, callback: Callable[[_T], Any], key: _KT) -> KeyedRef[_KT, _T]: ... # type: ignore
70+
def __new__(type: type[Self], ob: _T, callback: Callable[[_T], Any], key: _KT) -> Self: ... # type: ignore
7171
def __init__(self, ob: _T, callback: Callable[[_T], Any], key: _KT) -> None: ...
7272

7373
class WeakKeyDictionary(MutableMapping[_KT, _VT]):

0 commit comments

Comments
 (0)