Skip to content

Commit 41bf6a1

Browse files
mslapekJelleZijlstra
authored andcommitted
Refactor warnings.catch_warning to be a class. (#3499)
1 parent c76a298 commit 41bf6a1

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

stdlib/2and3/warnings.pyi

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Stubs for warnings
22

33
import sys
4-
from typing import Any, Dict, List, NamedTuple, Optional, overload, TextIO, Tuple, Type, Union, ContextManager
5-
from types import ModuleType
4+
from typing import Any, Dict, List, NamedTuple, Optional, overload, TextIO, Tuple, Type, Union
5+
from types import ModuleType, TracebackType
66

77
if sys.version_info >= (3, 8):
88
from typing import Literal
@@ -43,12 +43,20 @@ class _Record(NamedTuple):
4343
file: Optional[TextIO]
4444
line: Optional[str]
4545

46-
47-
@overload
48-
def catch_warnings(*, record: Literal[False] = ..., module: Optional[ModuleType] = ...) -> ContextManager[None]: ...
49-
50-
@overload
51-
def catch_warnings(*, record: Literal[True], module: Optional[ModuleType] = ...) -> ContextManager[List[_Record]]: ...
52-
53-
@overload
54-
def catch_warnings(*, record: bool, module: Optional[ModuleType] = ...) -> ContextManager[Optional[List[_Record]]]: ...
46+
class catch_warnings:
47+
@overload
48+
def __new__(cls, *, record: Literal[False] = ..., module: Optional[ModuleType] = ...) -> _catch_warnings_without_records: ...
49+
@overload
50+
def __new__(cls, *, record: Literal[True], module: Optional[ModuleType] = ...) -> _catch_warnings_with_records: ...
51+
@overload
52+
def __new__(cls, *, record: bool, module: Optional[ModuleType] = ...) -> catch_warnings: ...
53+
def __enter__(self) -> Optional[List[_Record]]: ...
54+
def __exit__(self, exc_type: Optional[Type[BaseException]],
55+
exc_val: Optional[BaseException],
56+
exc_tb: Optional[TracebackType]) -> None: ...
57+
58+
class _catch_warnings_without_records(catch_warnings):
59+
def __enter__(self) -> None: ...
60+
61+
class _catch_warnings_with_records(catch_warnings):
62+
def __enter__(self) -> List[_Record]: ...

0 commit comments

Comments
 (0)