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.
This commit is contained in:
Michael Lee
2019-08-16 16:13:33 -07:00
committed by Jelle Zijlstra
parent 3cfc3150f0
commit b294782183
25 changed files with 55 additions and 38 deletions

View File

@@ -53,7 +53,7 @@ class Executor:
def map(self, func: Callable[..., _T], *iterables: Iterable[Any], timeout: Optional[float] = ...,) -> Iterator[_T]: ...
def shutdown(self, wait: bool = ...) -> None: ...
def __enter__(self: _T) -> _T: ...
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool: ...
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Optional[bool]: ...
def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ...

View File

@@ -80,8 +80,7 @@ responses: Dict[int, str]
class HTTPMessage(email.message.Message): ...
# Ignore errors to work around python/mypy#5027
class HTTPResponse(io.BufferedIOBase, BinaryIO): # type: ignore
class HTTPResponse(io.BufferedIOBase, BinaryIO):
msg: HTTPMessage
headers: HTTPMessage
version: int
@@ -103,7 +102,7 @@ class HTTPResponse(io.BufferedIOBase, BinaryIO): # type: ignore
def __enter__(self) -> HTTPResponse: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[types.TracebackType]) -> bool: ...
exc_tb: Optional[types.TracebackType]) -> Optional[bool]: ...
def info(self) -> email.message.Message: ...
def geturl(self) -> str: ...
def getcode(self) -> int: ...

View File

@@ -28,7 +28,7 @@ class IOBase:
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]) -> bool: ...
exc_tb: Optional[TracebackType]) -> Optional[bool]: ...
def close(self) -> None: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...
@@ -86,7 +86,7 @@ class BytesIO(BinaryIO):
def __next__(self) -> bytes: ...
def __enter__(self) -> BytesIO: ...
def __exit__(self, t: Optional[Type[BaseException]] = ..., value: Optional[BaseException] = ...,
traceback: Optional[TracebackType] = ...) -> bool: ...
traceback: Optional[TracebackType] = ...) -> Optional[bool]: ...
def close(self) -> None: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...
@@ -165,7 +165,7 @@ class TextIOWrapper(TextIO):
) -> None: ...
# copied from IOBase
def __exit__(self, t: Optional[Type[BaseException]] = ..., value: Optional[BaseException] = ...,
traceback: Optional[TracebackType] = ...) -> bool: ...
traceback: Optional[TracebackType] = ...) -> Optional[bool]: ...
def close(self) -> None: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...

View File

@@ -38,7 +38,7 @@ class BaseServer:
def __enter__(self) -> BaseServer: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[types.TracebackType]) -> bool: ...
exc_tb: Optional[types.TracebackType]) -> None: ...
if sys.version_info >= (3, 3):
def service_actions(self) -> None: ...

View File

@@ -1170,7 +1170,7 @@ class Popen(Generic[AnyStr]):
def terminate(self) -> None: ...
def kill(self) -> None: ...
def __enter__(self) -> Popen: ...
def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> bool: ...
def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: ...
# The result really is always a str.
def getstatusoutput(cmd: _TXT) -> Tuple[int, str]: ...

View File

@@ -38,7 +38,7 @@ class SpooledTemporaryFile(IO[AnyStr]):
def __enter__(self) -> SpooledTemporaryFile: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> bool: ...
exc_tb: Optional[TracebackType]) -> Optional[bool]: ...
# These methods are copied from the abstract methods of IO, because
# SpooledTemporaryFile implements IO.
@@ -69,7 +69,7 @@ class TemporaryDirectory(Generic[AnyStr]):
def __enter__(self) -> AnyStr: ...
def __exit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> bool: ...
exc_tb: Optional[TracebackType]) -> None: ...
def mkstemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., dir: Optional[AnyStr] = ...,
text: bool = ...) -> Tuple[int, AnyStr]: ...

View File

@@ -479,7 +479,7 @@ class IO(Iterator[AnyStr], Generic[AnyStr]):
def __enter__(self) -> IO[AnyStr]: ...
@abstractmethod
def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException],
traceback: Optional[TracebackType]) -> bool: ...
traceback: Optional[TracebackType]) -> Optional[bool]: ...
class BinaryIO(IO[bytes]):
# TODO readinto

View File

@@ -214,11 +214,11 @@ class _AssertWarnsContext:
lineno: int
def __enter__(self) -> _AssertWarnsContext: ...
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> bool: ...
exc_tb: Optional[TracebackType]) -> None: ...
class _AssertLogsContext:
records: List[logging.LogRecord]
output: List[str]
def __enter__(self) -> _AssertLogsContext: ...
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> bool: ...
exc_tb: Optional[TracebackType]) -> Optional[bool]: ...

View File

@@ -7,7 +7,7 @@ _AIUT = TypeVar("_AIUT", bound=addbase)
class addbase(BinaryIO):
def __enter__(self: _AIUT) -> _AIUT: ...
def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> bool: ...
def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: ...
def __iter__(self: _AIUT) -> _AIUT: ...
def __next__(self) -> bytes: ...
def close(self) -> None: ...