diff --git a/stdlib/@python2/_io.pyi b/stdlib/@python2/_io.pyi index 4eafef332..2d825b07b 100644 --- a/stdlib/@python2/_io.pyi +++ b/stdlib/@python2/_io.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from mmap import mmap from typing import IO, Any, BinaryIO, Iterable, List, Optional, Text, TextIO, Tuple, Type, TypeVar, Union @@ -30,7 +31,7 @@ class _IOBase(BinaryIO): def tell(self) -> int: ... def truncate(self, size: Optional[int] = ...) -> int: ... def writable(self) -> bool: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, t: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[Any] ) -> Optional[bool]: ... @@ -56,7 +57,7 @@ class _BufferedIOBase(_IOBase): class BufferedRWPair(_BufferedIOBase): def __init__(self, reader: _RawIOBase, writer: _RawIOBase, buffer_size: int = ..., max_buffer_size: int = ...) -> None: ... def peek(self, n: int = ...) -> bytes: ... - def __enter__(self) -> BufferedRWPair: ... + def __enter__(self: Self) -> Self: ... class BufferedRandom(_BufferedIOBase): mode: str @@ -140,7 +141,7 @@ class _TextIOBase(TextIO): def writable(self) -> bool: ... def write(self, pbuf: unicode) -> int: ... def writelines(self, lines: Iterable[unicode]) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, t: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[Any] ) -> Optional[bool]: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 428139f42..0692d00a3 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -7,6 +7,7 @@ from _typeshed import ( OpenBinaryModeWriting, OpenTextMode, ReadableBuffer, + Self, StrOrBytesPath, SupportsDivMod, SupportsKeysAndGetItem, @@ -648,7 +649,7 @@ class memoryview(Sized, Sequence[int]): contiguous: bool nbytes: int def __init__(self, obj: ReadableBuffer) -> None: ... - def __enter__(self) -> memoryview: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> None: ... diff --git a/stdlib/bz2.pyi b/stdlib/bz2.pyi index acbbbbbf5..20fee8aa0 100644 --- a/stdlib/bz2.pyi +++ b/stdlib/bz2.pyi @@ -1,7 +1,7 @@ import _compression import sys from _compression import BaseStream -from _typeshed import ReadableBuffer, StrOrBytesPath, WriteableBuffer +from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer from typing import IO, Any, Iterable, List, Optional, Protocol, TextIO, TypeVar, Union, overload from typing_extensions import Literal, SupportsIndex @@ -82,7 +82,7 @@ def open( ) -> TextIO: ... class BZ2File(BaseStream, IO[bytes]): - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... if sys.version_info >= (3, 9): @overload def __init__(self, filename: _WritableFileobj, mode: _WriteBinaryMode, *, compresslevel: int = ...) -> None: ... diff --git a/stdlib/cProfile.pyi b/stdlib/cProfile.pyi index 6648ed5b9..a1deb38df 100644 --- a/stdlib/cProfile.pyi +++ b/stdlib/cProfile.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import StrOrBytesPath +from _typeshed import Self, StrOrBytesPath from types import CodeType from typing import Any, Callable, Dict, Optional, Tuple, TypeVar, Union @@ -8,7 +8,6 @@ def runctx( statement: str, globals: Dict[str, Any], locals: Dict[str, Any], filename: Optional[str] = ..., sort: Union[str, int] = ... ) -> None: ... -_SelfT = TypeVar("_SelfT", bound=Profile) _T = TypeVar("_T") _Label = Tuple[str, int, str] @@ -23,11 +22,11 @@ class Profile: def dump_stats(self, file: StrOrBytesPath) -> None: ... def create_stats(self) -> None: ... def snapshot_stats(self) -> None: ... - def run(self: _SelfT, cmd: str) -> _SelfT: ... - def runctx(self: _SelfT, cmd: str, globals: Dict[str, Any], locals: Dict[str, Any]) -> _SelfT: ... + def run(self: Self, cmd: str) -> Self: ... + def runctx(self: Self, cmd: str, globals: Dict[str, Any], locals: Dict[str, Any]) -> Self: ... def runcall(self, __func: Callable[..., _T], *args: Any, **kw: Any) -> _T: ... if sys.version_info >= (3, 8): - def __enter__(self: _SelfT) -> _SelfT: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *exc_info: Any) -> None: ... def label(code: Union[str, CodeType]) -> _Label: ... # undocumented diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index ab83caf18..3e61c9eaf 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -1,5 +1,6 @@ import sys import types +from _typeshed import Self from abc import abstractmethod from typing import ( IO, @@ -188,8 +189,6 @@ class BufferedIncrementalDecoder(IncrementalDecoder): def _buffer_decode(self, input: bytes, errors: str, final: bool) -> Tuple[str, int]: ... def decode(self, input: bytes, final: bool = ...) -> str: ... -_SW = TypeVar("_SW", bound=StreamWriter) - # TODO: it is not possible to specify the requirement that all other # attributes and methods are passed-through from the stream. class StreamWriter(Codec): @@ -198,14 +197,12 @@ class StreamWriter(Codec): def write(self, object: str) -> None: ... def writelines(self, list: Iterable[str]) -> None: ... def reset(self) -> None: ... - def __enter__(self: _SW) -> _SW: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType] ) -> None: ... def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ... -_SR = TypeVar("_SR", bound=StreamReader) - class StreamReader(Codec): errors: str def __init__(self, stream: IO[bytes], errors: str = ...) -> None: ... @@ -213,7 +210,7 @@ class StreamReader(Codec): def readline(self, size: Optional[int] = ..., keepends: bool = ...) -> str: ... def readlines(self, sizehint: Optional[int] = ..., keepends: bool = ...) -> List[str]: ... def reset(self) -> None: ... - def __enter__(self: _SR) -> _SR: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType] ) -> None: ... @@ -237,7 +234,7 @@ class StreamReaderWriter(TextIO): def reset(self) -> None: ... # Same as write() def seek(self, offset: int, whence: int = ...) -> int: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType] ) -> None: ... @@ -275,7 +272,7 @@ class StreamRecoder(BinaryIO): def writelines(self, list: Iterable[bytes]) -> int: ... # type: ignore # it's supposed to return None def reset(self) -> None: ... def __getattr__(self, name: str) -> Any: ... - def __enter__(self: _SRT) -> _SRT: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, type: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[types.TracebackType] ) -> None: ... diff --git a/stdlib/contextlib.pyi b/stdlib/contextlib.pyi index 66e7262d3..72cd757e0 100644 --- a/stdlib/contextlib.pyi +++ b/stdlib/contextlib.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import Self from types import TracebackType from typing import ( IO, @@ -80,7 +81,7 @@ class ExitStack(ContextManager[ExitStack]): def callback(self, callback: Callable[..., Any], *args: Any, **kwds: Any) -> Callable[..., Any]: ... def pop_all(self: _U) -> _U: ... def close(self) -> None: ... - def __enter__(self: _U) -> _U: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, __exc_type: Optional[Type[BaseException]], diff --git a/stdlib/fileinput.pyi b/stdlib/fileinput.pyi index 0fd7937e1..9614ce108 100644 --- a/stdlib/fileinput.pyi +++ b/stdlib/fileinput.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import StrOrBytesPath +from _typeshed import Self, StrOrBytesPath from typing import IO, Any, AnyStr, Callable, Generic, Iterable, Iterator, Union if sys.version_info >= (3, 10): @@ -78,7 +78,7 @@ class FileInput(Iterable[AnyStr], Generic[AnyStr]): ) -> None: ... def __del__(self) -> None: ... def close(self) -> None: ... - def __enter__(self) -> FileInput[AnyStr]: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ... def __iter__(self) -> Iterator[AnyStr]: ... def __next__(self) -> AnyStr: ... diff --git a/stdlib/ftplib.pyi b/stdlib/ftplib.pyi index c474da2e3..ba2c458c4 100644 --- a/stdlib/ftplib.pyi +++ b/stdlib/ftplib.pyi @@ -1,12 +1,10 @@ -from _typeshed import SupportsRead, SupportsReadline +from _typeshed import Self, SupportsRead, SupportsReadline from socket import socket from ssl import SSLContext from types import TracebackType -from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, TextIO, Tuple, Type, TypeVar, Union +from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, TextIO, Tuple, Type, Union from typing_extensions import Literal -_T = TypeVar("_T") - MSG_OOB: int FTP_PORT: int MAXLINE: int @@ -34,7 +32,7 @@ class FTP: lastresp: str file: Optional[TextIO] encoding: str - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> None: ...