Skip to content

Use _typeshed.Self with __enter__ #5717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions stdlib/io.pyi
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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

SEEK_SET: int
SEEK_CUR: int
SEEK_END: int

_T = TypeVar("_T", bound=IOBase)

open = builtins.open

if sys.version_info >= (3, 8):
Expand All @@ -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]: ...
Expand Down Expand Up @@ -79,15 +77,15 @@ 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: ...
# BytesIO does not contain a "name" field. This workaround is necessary
# 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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions stdlib/lzma.pyi
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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
Expand Down Expand Up @@ -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: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/mailbox.pyi
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/nntplib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]]: ...
Expand Down
12 changes: 10 additions & 2 deletions stdlib/pathlib.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]: ...
Expand Down
9 changes: 4 additions & 5 deletions stdlib/runpy.pyi
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
4 changes: 2 additions & 2 deletions stdlib/select.pyi
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]] = ...,
Expand Down
4 changes: 2 additions & 2 deletions stdlib/selectors.pyi
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions stdlib/socket.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/socketserver.pyi
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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: ...
Expand Down
5 changes: 2 additions & 3 deletions stdlib/subprocess.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]):
Expand Down Expand Up @@ -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: ...
Expand Down
5 changes: 3 additions & 2 deletions stdlib/sunau.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from _typeshed import Self
from typing import IO, Any, NamedTuple, NoReturn, Optional, Union

_File = Union[str, IO[bytes]]
Expand Down Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand Down