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.
47 lines
2.3 KiB
Python
47 lines
2.3 KiB
Python
# Stubs for warnings
|
|
|
|
from typing import Any, Dict, List, NamedTuple, Optional, overload, TextIO, Tuple, Type, Union
|
|
from types import ModuleType, TracebackType
|
|
|
|
@overload
|
|
def warn(message: str, category: Optional[Type[Warning]] = ..., stacklevel: int = ...) -> None: ...
|
|
@overload
|
|
def warn(message: Warning, category: Any = ..., stacklevel: int = ...) -> None: ...
|
|
@overload
|
|
def warn_explicit(message: str, category: Type[Warning],
|
|
filename: str, lineno: int, module: Optional[str] = ...,
|
|
registry: Optional[Dict[Union[str, Tuple[str, Type[Warning], int]], int]] = ...,
|
|
module_globals: Optional[Dict[str, Any]] = ...) -> None: ...
|
|
@overload
|
|
def warn_explicit(message: Warning, category: Any,
|
|
filename: str, lineno: int, module: Optional[str] = ...,
|
|
registry: Optional[Dict[Union[str, Tuple[str, Type[Warning], int]], int]] = ...,
|
|
module_globals: Optional[Dict[str, Any]] = ...) -> None: ...
|
|
def showwarning(message: str, category: Type[Warning], filename: str,
|
|
lineno: int, file: Optional[TextIO] = ...,
|
|
line: Optional[str] = ...) -> None: ...
|
|
def formatwarning(message: str, category: Type[Warning], filename: str,
|
|
lineno: int, line: Optional[str] = ...) -> str: ...
|
|
def filterwarnings(action: str, message: str = ...,
|
|
category: Type[Warning] = ..., module: str = ...,
|
|
lineno: int = ..., append: bool = ...) -> None: ...
|
|
def simplefilter(action: str, category: Type[Warning] = ..., lineno: int = ...,
|
|
append: bool = ...) -> None: ...
|
|
def resetwarnings() -> None: ...
|
|
|
|
_Record = NamedTuple('_Record',
|
|
[('message', str),
|
|
('category', Type[Warning]),
|
|
('filename', str),
|
|
('lineno', int),
|
|
('file', Optional[TextIO]),
|
|
('line', Optional[str])])
|
|
|
|
class catch_warnings:
|
|
def __init__(self, *, record: bool = ...,
|
|
module: Optional[ModuleType] = ...) -> None: ...
|
|
def __enter__(self) -> Optional[List[_Record]]: ...
|
|
def __exit__(self, exc_type: Optional[Type[BaseException]],
|
|
exc_val: Optional[BaseException],
|
|
exc_tb: Optional[TracebackType]) -> None: ...
|