Files
typeshed/stdlib/2/tempfile.pyi
Michael Lee b294782183 Make most contextmanager __exit__ signatures return Optional[bool] (#3179)
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.
2019-08-16 16:13:33 -07:00

112 lines
3.7 KiB
Python

from typing import Any, AnyStr, IO, Iterable, Iterator, List, Optional, overload, Text, Tuple, Union
from thread import LockType
from random import Random
TMP_MAX: int
tempdir: str
template: str
_name_sequence: Optional[_RandomNameSequence]
class _RandomNameSequence:
characters: str = ...
mutex: LockType
@property
def rng(self) -> Random: ...
def __iter__(self) -> _RandomNameSequence: ...
def next(self) -> str: ...
# from os.path:
def normcase(self, path: AnyStr) -> AnyStr: ...
class _TemporaryFileWrapper(IO[str]):
delete: bool
file: IO
name: Any
def __init__(self, file: IO, name: Any, delete: bool = ...) -> None: ...
def __del__(self) -> None: ...
def __enter__(self) -> _TemporaryFileWrapper: ...
def __exit__(self, exc, value, tb) -> Optional[bool]: ...
def __getattr__(self, name: unicode) -> Any: ...
def close(self) -> None: ...
def unlink(self, path: unicode) -> None: ...
# These methods don't exist directly on this object, but
# are delegated to the underlying IO object through __getattr__.
# We need to add them here so that this class is concrete.
def __iter__(self) -> Iterator[str]: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...
def isatty(self) -> bool: ...
def next(self) -> str: ...
def read(self, n: int = ...) -> str: ...
def readable(self) -> bool: ...
def readline(self, limit: int = ...) -> str: ...
def readlines(self, hint: int = ...) -> List[str]: ...
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 write(self, s: Text) -> int: ...
def writelines(self, lines: Iterable[str]) -> None: ...
# TODO text files
def TemporaryFile(
mode: Union[bytes, unicode] = ...,
bufsize: int = ...,
suffix: Union[bytes, unicode] = ...,
prefix: Union[bytes, unicode] = ...,
dir: Union[bytes, unicode] = ...
) -> _TemporaryFileWrapper:
...
def NamedTemporaryFile(
mode: Union[bytes, unicode] = ...,
bufsize: int = ...,
suffix: Union[bytes, unicode] = ...,
prefix: Union[bytes, unicode] = ...,
dir: Union[bytes, unicode] = ...,
delete: bool = ...
) -> _TemporaryFileWrapper:
...
def SpooledTemporaryFile(
max_size: int = ...,
mode: Union[bytes, unicode] = ...,
buffering: int = ...,
suffix: Union[bytes, unicode] = ...,
prefix: Union[bytes, unicode] = ...,
dir: Union[bytes, unicode] = ...
) -> _TemporaryFileWrapper:
...
class TemporaryDirectory:
name: Any
def __init__(self,
suffix: Union[bytes, unicode] = ...,
prefix: Union[bytes, unicode] = ...,
dir: Union[bytes, unicode] = ...) -> None: ...
def cleanup(self) -> None: ...
def __enter__(self) -> Any: ... # Can be str or unicode
def __exit__(self, type, value, traceback) -> None: ...
@overload
def mkstemp() -> Tuple[int, str]: ...
@overload
def mkstemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: Optional[AnyStr] = ...,
text: bool = ...) -> Tuple[int, AnyStr]: ...
@overload
def mkdtemp() -> str: ...
@overload
def mkdtemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: Optional[AnyStr] = ...) -> AnyStr: ...
@overload
def mktemp() -> str: ...
@overload
def mktemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: Optional[AnyStr] = ...) -> AnyStr: ...
def gettempdir() -> str: ...
def gettempprefix() -> str: ...
def _candidate_tempdir_list() -> List[str]: ...
def _get_candidate_names() -> Optional[_RandomNameSequence]: ...
def _get_default_tempdir() -> str: ...