mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-06 12:14:27 +08:00
This pull request is a follow-up to https://github.com/python/mypy/issues/7214. In short, within that mypy issue, we found it would be helpful to determine between contextmanagers that can "swallow" exceptions vs ones that can't. This helps prevent some false positive when using flags that analyze control flow such as `--warn-unreachable`. To do this, Jelle proposed assuming that only contextmanagers where the `__exit__` returns `bool` are assumed to swallow exceptions. This unfortunately required the following typeshed changes: 1. The typing.IO, threading.Lock, and concurrent.futures.Executor were all modified so `__exit__` returns `Optional[None]` instead of None -- along with all of their subclasses. I believe these three types are meant to be subclassed, so I felt picking the more general type was correct. 2. There were also a few concrete types (e.g. see socketserver, subprocess, ftplib...) that I modified to return `None` -- I checked the source code, and these all seem to return None (and don't appear to be meant to be subclassable). 3. contextlib.suppress was changed to return bool. I also double-checked the unittest modules and modified a subset of those contextmanagers, leaving ones like `_AssertRaisesContext` alone.
209 lines
8.0 KiB
Python
209 lines
8.0 KiB
Python
from typing import (
|
|
List, BinaryIO, TextIO, Iterator, Union, Optional, Callable, Tuple, Type, Any, IO, Iterable
|
|
)
|
|
import builtins
|
|
import codecs
|
|
from mmap import mmap
|
|
import sys
|
|
from types import TracebackType
|
|
from typing import TypeVar
|
|
|
|
_bytearray_like = Union[bytearray, mmap]
|
|
|
|
DEFAULT_BUFFER_SIZE: int
|
|
|
|
SEEK_SET: int
|
|
SEEK_CUR: int
|
|
SEEK_END: int
|
|
|
|
_T = TypeVar('_T', bound='IOBase')
|
|
|
|
open = builtins.open
|
|
|
|
BlockingIOError = builtins.BlockingIOError
|
|
class UnsupportedOperation(OSError, ValueError): ...
|
|
|
|
class IOBase:
|
|
def __iter__(self) -> Iterator[bytes]: ...
|
|
def __next__(self) -> bytes: ...
|
|
def __enter__(self: _T) -> _T: ...
|
|
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException],
|
|
exc_tb: Optional[TracebackType]) -> Optional[bool]: ...
|
|
def close(self) -> None: ...
|
|
def fileno(self) -> int: ...
|
|
def flush(self) -> None: ...
|
|
def isatty(self) -> bool: ...
|
|
def readable(self) -> bool: ...
|
|
def readlines(self, hint: int = ...) -> List[bytes]: ...
|
|
def seek(self, offset: int, whence: int = ...) -> int: ...
|
|
def seekable(self) -> bool: ...
|
|
def tell(self) -> int: ...
|
|
def truncate(self, size: Optional[int] = ...) -> int: ...
|
|
def writable(self) -> bool: ...
|
|
def writelines(self, lines: Iterable[Union[bytes, bytearray]]) -> None: ...
|
|
def readline(self, size: int = ...) -> bytes: ...
|
|
def __del__(self) -> None: ...
|
|
@property
|
|
def closed(self) -> bool: ...
|
|
|
|
class RawIOBase(IOBase):
|
|
def readall(self) -> bytes: ...
|
|
def readinto(self, b: bytearray) -> Optional[int]: ...
|
|
def write(self, b: Union[bytes, bytearray]) -> Optional[int]: ...
|
|
def read(self, size: int = ...) -> Optional[bytes]: ...
|
|
|
|
class BufferedIOBase(IOBase):
|
|
def detach(self) -> RawIOBase: ...
|
|
def readinto(self, b: _bytearray_like) -> int: ...
|
|
def write(self, b: Union[bytes, bytearray]) -> int: ...
|
|
def readinto1(self, b: _bytearray_like) -> int: ...
|
|
def read(self, size: Optional[int] = ...) -> bytes: ...
|
|
def read1(self, size: int = ...) -> bytes: ...
|
|
|
|
|
|
class FileIO(RawIOBase):
|
|
mode: str
|
|
name: Union[int, str]
|
|
def __init__(
|
|
self,
|
|
name: Union[str, bytes, int],
|
|
mode: str = ...,
|
|
closefd: bool = ...,
|
|
opener: Optional[Callable[[Union[int, str], str], int]] = ...
|
|
) -> None: ...
|
|
|
|
# TODO should extend from BufferedIOBase
|
|
class BytesIO(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 getvalue(self) -> bytes: ...
|
|
def getbuffer(self) -> memoryview: ...
|
|
# copied from IOBase
|
|
def __iter__(self) -> Iterator[bytes]: ...
|
|
def __next__(self) -> bytes: ...
|
|
def __enter__(self) -> BytesIO: ...
|
|
def __exit__(self, t: Optional[Type[BaseException]] = ..., value: Optional[BaseException] = ...,
|
|
traceback: Optional[TracebackType] = ...) -> Optional[bool]: ...
|
|
def close(self) -> None: ...
|
|
def fileno(self) -> int: ...
|
|
def flush(self) -> None: ...
|
|
def isatty(self) -> bool: ...
|
|
def readable(self) -> bool: ...
|
|
def readlines(self, hint: int = ...) -> List[bytes]: ...
|
|
def seek(self, offset: int, whence: int = ...) -> int: ...
|
|
def seekable(self) -> bool: ...
|
|
def tell(self) -> int: ...
|
|
def truncate(self, size: Optional[int] = ...) -> int: ...
|
|
def writable(self) -> bool: ...
|
|
# TODO should be the next line instead
|
|
# def writelines(self, lines: List[Union[bytes, bytearray]]) -> None: ...
|
|
def writelines(self, lines: Any) -> None: ...
|
|
def readline(self, size: int = ...) -> bytes: ...
|
|
def __del__(self) -> None: ...
|
|
closed: bool
|
|
# copied from BufferedIOBase
|
|
def detach(self) -> RawIOBase: ...
|
|
def readinto(self, b: _bytearray_like) -> int: ...
|
|
def write(self, b: Union[bytes, bytearray]) -> int: ...
|
|
def readinto1(self, b: _bytearray_like) -> int: ...
|
|
def read(self, size: Optional[int] = ...) -> bytes: ...
|
|
def read1(self, size: int = ...) -> bytes: ...
|
|
|
|
class BufferedReader(BufferedIOBase):
|
|
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
|
|
def peek(self, size: int = ...) -> bytes: ...
|
|
|
|
class BufferedWriter(BufferedIOBase):
|
|
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
|
|
def flush(self) -> None: ...
|
|
def write(self, b: Union[bytes, bytearray]) -> int: ...
|
|
|
|
class BufferedRandom(BufferedReader, BufferedWriter):
|
|
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
|
|
def seek(self, offset: int, whence: int = ...) -> int: ...
|
|
def tell(self) -> int: ...
|
|
|
|
class BufferedRWPair(BufferedIOBase):
|
|
def __init__(self, reader: RawIOBase, writer: RawIOBase,
|
|
buffer_size: int = ...) -> None: ...
|
|
|
|
|
|
class TextIOBase(IOBase):
|
|
encoding: str
|
|
errors: Optional[str]
|
|
newlines: Union[str, Tuple[str, ...], None]
|
|
def __iter__(self) -> Iterator[str]: ... # type: ignore
|
|
def __next__(self) -> str: ... # type: ignore
|
|
def detach(self) -> IOBase: ...
|
|
def write(self, s: str) -> int: ...
|
|
def writelines(self, lines: List[str]) -> None: ... # type: ignore
|
|
def readline(self, size: int = ...) -> str: ... # type: ignore
|
|
def readlines(self, hint: int = ...) -> List[str]: ... # type: ignore
|
|
def read(self, size: Optional[int] = ...) -> str: ...
|
|
def seek(self, offset: int, whence: int = ...) -> int: ...
|
|
def tell(self) -> int: ...
|
|
|
|
# TODO should extend from TextIOBase
|
|
class TextIOWrapper(TextIO):
|
|
line_buffering: bool
|
|
# TODO uncomment after fixing mypy about using write_through
|
|
# def __init__(self, buffer: IO[bytes], encoding: str = ...,
|
|
# errors: Optional[str] = ..., newline: Optional[str] = ...,
|
|
# line_buffering: bool = ..., write_through: bool = ...) \
|
|
# -> None: ...
|
|
def __init__(
|
|
self,
|
|
buffer: IO[bytes],
|
|
encoding: Optional[str] = ...,
|
|
errors: Optional[str] = ...,
|
|
newline: Optional[str] = ...,
|
|
line_buffering: bool = ...,
|
|
write_through: bool = ...
|
|
) -> None: ...
|
|
# copied from IOBase
|
|
def __exit__(self, t: Optional[Type[BaseException]] = ..., value: Optional[BaseException] = ...,
|
|
traceback: Optional[TracebackType] = ...) -> Optional[bool]: ...
|
|
def close(self) -> None: ...
|
|
def fileno(self) -> int: ...
|
|
def flush(self) -> None: ...
|
|
def isatty(self) -> bool: ...
|
|
def readable(self) -> bool: ...
|
|
def readlines(self, hint: int = ...) -> List[str]: ...
|
|
def seekable(self) -> bool: ...
|
|
def truncate(self, size: Optional[int] = ...) -> int: ...
|
|
def writable(self) -> bool: ...
|
|
# TODO should be the next line instead
|
|
# def writelines(self, lines: List[str]) -> None: ...
|
|
def writelines(self, lines: Any) -> None: ...
|
|
def __del__(self) -> None: ...
|
|
closed: bool
|
|
# copied from TextIOBase
|
|
encoding: str
|
|
errors: Optional[str]
|
|
newlines: Union[str, Tuple[str, ...], None]
|
|
def __iter__(self) -> Iterator[str]: ...
|
|
def __next__(self) -> str: ...
|
|
def __enter__(self) -> TextIO: ...
|
|
def detach(self) -> IOBase: ...
|
|
def write(self, s: str) -> int: ...
|
|
def readline(self, size: int = ...) -> str: ...
|
|
def read(self, size: Optional[int] = ...) -> str: ...
|
|
def seek(self, offset: int, whence: int = ...) -> int: ...
|
|
def tell(self) -> int: ...
|
|
|
|
class StringIO(TextIOWrapper):
|
|
def __init__(self, initial_value: str = ...,
|
|
newline: Optional[str] = ...) -> None: ...
|
|
# StringIO does not contain a "name" field. This workaround is necessary
|
|
# to allow StringIO sub-classes to add this field, as it is defined
|
|
# as a read-only property on IO[].
|
|
name: Any
|
|
def getvalue(self) -> str: ...
|
|
def __enter__(self) -> StringIO: ...
|
|
|
|
class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
|
|
def decode(self, input: bytes, final: bool = ...) -> str: ...
|