diff --git a/stdlib/io.pyi b/stdlib/io.pyi index f03a9a8740fe..c362a8587512 100644 --- a/stdlib/io.pyi +++ b/stdlib/io.pyi @@ -1,9 +1,9 @@ import builtins import codecs import sys -from _typeshed import ReadableBuffer, WriteableBuffer +from _typeshed import ReadableBuffer, Self, WriteableBuffer from types import TracebackType -from typing import IO, Any, BinaryIO, Callable, Iterable, Iterator, List, Optional, TextIO, Tuple, Type, TypeVar, Union +from typing import IO, Any, BinaryIO, Callable, Iterable, Iterator, List, Optional, TextIO, Tuple, Type, Union DEFAULT_BUFFER_SIZE: int @@ -11,8 +11,6 @@ SEEK_SET: int SEEK_CUR: int SEEK_END: int -_T = TypeVar("_T", bound=IOBase) - open = builtins.open if sys.version_info >= (3, 8): @@ -25,7 +23,7 @@ class UnsupportedOperation(OSError, ValueError): ... class IOBase: def __iter__(self) -> Iterator[bytes]: ... def __next__(self) -> bytes: ... - 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] ) -> Optional[bool]: ... @@ -79,7 +77,7 @@ class FileIO(RawIOBase, BinaryIO): def closefd(self) -> bool: ... def write(self, __b: ReadableBuffer) -> int: ... def read(self, __size: int = ...) -> bytes: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... class BytesIO(BufferedIOBase, BinaryIO): def __init__(self, initial_bytes: bytes = ...) -> None: ... @@ -87,7 +85,7 @@ class BytesIO(BufferedIOBase, BinaryIO): # to allow BytesIO sub-classes to add this field, as it is defined # as a read-only property on IO[]. name: Any - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def getvalue(self) -> bytes: ... def getbuffer(self) -> memoryview: ... if sys.version_info >= (3, 7): @@ -96,7 +94,7 @@ class BytesIO(BufferedIOBase, BinaryIO): def read1(self, __size: Optional[int]) -> bytes: ... # type: ignore class BufferedReader(BufferedIOBase, BinaryIO): - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ... def peek(self, __size: int = ...) -> bytes: ... if sys.version_info >= (3, 7): @@ -105,12 +103,12 @@ class BufferedReader(BufferedIOBase, BinaryIO): def read1(self, __size: int) -> bytes: ... # type: ignore class BufferedWriter(BufferedIOBase, BinaryIO): - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ... def write(self, __buffer: ReadableBuffer) -> int: ... class BufferedRandom(BufferedReader, BufferedWriter): - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ... def seek(self, __target: int, __whence: int = ...) -> int: ... if sys.version_info >= (3, 7): @@ -165,7 +163,7 @@ class TextIOWrapper(TextIOBase, TextIO): write_through: Optional[bool] = ..., ) -> None: ... # These are inherited from TextIOBase, but must exist in the stub to satisfy mypy. - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __iter__(self) -> Iterator[str]: ... # type: ignore def __next__(self) -> str: ... # type: ignore def writelines(self, __lines: Iterable[str]) -> None: ... # type: ignore diff --git a/stdlib/lzma.pyi b/stdlib/lzma.pyi index 0228b9d12d55..1e4efd42a1a0 100644 --- a/stdlib/lzma.pyi +++ b/stdlib/lzma.pyi @@ -1,6 +1,6 @@ import io -from _typeshed import ReadableBuffer, StrOrBytesPath -from typing import IO, Any, Mapping, Optional, Sequence, TextIO, TypeVar, Union, overload +from _typeshed import ReadableBuffer, Self, StrOrBytesPath +from typing import IO, Any, Mapping, Optional, Sequence, TextIO, Union, overload from typing_extensions import Literal _OpenBinaryWritingMode = Literal["w", "wb", "x", "xb", "a", "ab"] @@ -9,7 +9,6 @@ _OpenTextWritingMode = Literal["wt", "xt", "at"] _PathOrFile = Union[StrOrBytesPath, IO[bytes]] _FilterChain = Sequence[Mapping[str, Any]] -_T = TypeVar("_T") FORMAT_AUTO: int FORMAT_XZ: int @@ -76,7 +75,7 @@ class LZMAFile(io.BufferedIOBase, IO[bytes]): preset: Optional[int] = ..., filters: Optional[_FilterChain] = ..., ) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def close(self) -> None: ... @property def closed(self) -> bool: ... diff --git a/stdlib/mailbox.pyi b/stdlib/mailbox.pyi index 1abe616fdc18..a5e9322347d6 100644 --- a/stdlib/mailbox.pyi +++ b/stdlib/mailbox.pyi @@ -1,6 +1,6 @@ import email.message import sys -from _typeshed import StrOrBytesPath +from _typeshed import Self, StrOrBytesPath from types import TracebackType from typing import ( IO, @@ -189,7 +189,7 @@ class _ProxyFile(Generic[AnyStr]): def tell(self) -> int: ... def seek(self, offset: int, whence: int = ...) -> None: ... def close(self) -> None: ... - def __enter__(self) -> _ProxyFile[AnyStr]: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[TracebackType] ) -> None: ... diff --git a/stdlib/nntplib.pyi b/stdlib/nntplib.pyi index 36fe063c8486..c3c981626fd9 100644 --- a/stdlib/nntplib.pyi +++ b/stdlib/nntplib.pyi @@ -2,9 +2,9 @@ import datetime import socket import ssl import sys -from typing import IO, Any, Dict, Iterable, List, NamedTuple, Optional, Tuple, TypeVar, Union +from _typeshed import Self +from typing import IO, Any, Dict, Iterable, List, NamedTuple, Optional, Tuple, Union -_SelfT = TypeVar("_SelfT", bound=_NNTPBase) _File = Union[IO[bytes], bytes, str, None] class NNTPError(Exception): @@ -46,7 +46,7 @@ class _NNTPBase: nntp_implementation: str nntp_version: int def __init__(self, file: IO[bytes], host: str, readermode: Optional[bool] = ..., timeout: float = ...) -> None: ... - def __enter__(self: _SelfT) -> _SelfT: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... def getwelcome(self) -> str: ... def getcapabilities(self) -> Dict[str, List[str]]: ... diff --git a/stdlib/pathlib.pyi b/stdlib/pathlib.pyi index b4624c3bea73..11c96fae6093 100644 --- a/stdlib/pathlib.pyi +++ b/stdlib/pathlib.pyi @@ -1,5 +1,13 @@ import sys -from _typeshed import OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode, StrPath +from _typeshed import ( + OpenBinaryMode, + OpenBinaryModeReading, + OpenBinaryModeUpdating, + OpenBinaryModeWriting, + OpenTextMode, + Self, + StrPath, +) from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper from os import PathLike, stat_result from types import TracebackType @@ -54,7 +62,7 @@ class PureWindowsPath(PurePath): ... class Path(PurePath): def __new__(cls: Type[_P], *args: StrPath, **kwargs: Any) -> _P: ... - def __enter__(self: _P) -> _P: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: Optional[TracebackType] ) -> Optional[bool]: ... diff --git a/stdlib/runpy.pyi b/stdlib/runpy.pyi index d40c3668ad2f..7df9bee7b557 100644 --- a/stdlib/runpy.pyi +++ b/stdlib/runpy.pyi @@ -1,19 +1,18 @@ +from _typeshed import Self from types import ModuleType -from typing import Any, Dict, Optional, TypeVar - -_T = TypeVar("_T") +from typing import Any, Dict, Optional class _TempModule: mod_name: str = ... module: ModuleType = ... def __init__(self, mod_name: str) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... class _ModifiedArgv0: value: Any = ... def __init__(self, value: Any) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self) -> None: ... def __exit__(self, *args: Any) -> None: ... def run_module( diff --git a/stdlib/select.pyi b/stdlib/select.pyi index 1a046c7b9bc3..0cdbaa5f735a 100644 --- a/stdlib/select.pyi +++ b/stdlib/select.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import FileDescriptorLike +from _typeshed import FileDescriptorLike, Self from types import TracebackType from typing import Any, Iterable, List, Optional, Tuple, Type @@ -101,7 +101,7 @@ if sys.platform != "linux" and sys.platform != "win32": if sys.platform == "linux": class epoll(object): def __init__(self, sizehint: int = ..., flags: int = ...) -> None: ... - def __enter__(self) -> epoll: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]] = ..., diff --git a/stdlib/selectors.pyi b/stdlib/selectors.pyi index 94690efadbf8..ade59f47e1bf 100644 --- a/stdlib/selectors.pyi +++ b/stdlib/selectors.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import FileDescriptor, FileDescriptorLike +from _typeshed import FileDescriptor, FileDescriptorLike, Self from abc import ABCMeta, abstractmethod from typing import Any, List, Mapping, NamedTuple, Optional, Tuple @@ -26,7 +26,7 @@ class BaseSelector(metaclass=ABCMeta): def get_key(self, fileobj: FileDescriptorLike) -> SelectorKey: ... @abstractmethod def get_map(self) -> Mapping[FileDescriptorLike, SelectorKey]: ... - def __enter__(self) -> BaseSelector: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... class SelectSelector(BaseSelector): diff --git a/stdlib/socket.pyi b/stdlib/socket.pyi index 139152a2f8a5..6bc1ae62fa16 100644 --- a/stdlib/socket.pyi +++ b/stdlib/socket.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import ReadableBuffer, WriteableBuffer +from _typeshed import ReadableBuffer, Self, WriteableBuffer from collections.abc import Iterable from enum import IntEnum, IntFlag from io import RawIOBase @@ -545,7 +545,7 @@ class socket(_socket.socket): proto: int = ..., fileno: Optional[int] = ..., ) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: object) -> None: ... def dup(self: _T) -> _T: ... # noqa: F811 def accept(self) -> tuple[socket, _RetAddress]: ... diff --git a/stdlib/socketserver.pyi b/stdlib/socketserver.pyi index 74bba86c554f..1403d9cee0ed 100644 --- a/stdlib/socketserver.pyi +++ b/stdlib/socketserver.pyi @@ -1,5 +1,6 @@ import sys import types +from _typeshed import Self from socket import socket as _socket from typing import Any, BinaryIO, Callable, ClassVar, Optional, Set, Tuple, Type, TypeVar, Union @@ -30,7 +31,7 @@ class BaseServer: def server_activate(self) -> None: ... def server_bind(self) -> None: ... def verify_request(self, request: _RequestType, client_address: _AddressType) -> bool: ... - 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[types.TracebackType] ) -> None: ... diff --git a/stdlib/subprocess.pyi b/stdlib/subprocess.pyi index 9a431f082c37..df63c5a0a709 100644 --- a/stdlib/subprocess.pyi +++ b/stdlib/subprocess.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import StrOrBytesPath +from _typeshed import Self, StrOrBytesPath from types import TracebackType from typing import IO, Any, AnyStr, Callable, Generic, Mapping, Optional, Sequence, Tuple, Type, TypeVar, Union, overload from typing_extensions import Literal @@ -34,7 +34,6 @@ if sys.platform == "win32": else: _ENV = Union[Mapping[bytes, StrOrBytesPath], Mapping[str, StrOrBytesPath]] -_S = TypeVar("_S") _T = TypeVar("_T") class CompletedProcess(Generic[_T]): @@ -1007,7 +1006,7 @@ class Popen(Generic[AnyStr]): def send_signal(self, sig: int) -> None: ... def terminate(self) -> None: ... def kill(self) -> None: ... - def __enter__(self: _S) -> _S: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType] ) -> None: ... diff --git a/stdlib/sunau.pyi b/stdlib/sunau.pyi index 79d0881c96c2..04a6832d8b44 100644 --- a/stdlib/sunau.pyi +++ b/stdlib/sunau.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import Self from typing import IO, Any, NamedTuple, NoReturn, Optional, Union _File = Union[str, IO[bytes]] @@ -30,7 +31,7 @@ class _sunau_params(NamedTuple): class Au_read: def __init__(self, f: _File) -> None: ... - def __enter__(self) -> Au_read: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... def getfp(self) -> Optional[IO[bytes]]: ... def rewind(self) -> None: ... @@ -50,7 +51,7 @@ class Au_read: class Au_write: def __init__(self, f: _File) -> None: ... - def __enter__(self) -> Au_write: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... def setnchannels(self, nchannels: int) -> None: ... def getnchannels(self) -> int: ...