Skip to content

Commit 34d68ab

Browse files
mslapeksrittau
authored andcommitted
Add warnings.catch_warning type hints with Literal. (#3464)
Closes: #3463
1 parent 8b6cd5c commit 34d68ab

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

stdlib/2and3/warnings.pyi

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Stubs for warnings
22

3-
from typing import Any, Dict, List, NamedTuple, Optional, overload, TextIO, Tuple, Type, Union
4-
from types import ModuleType, TracebackType
3+
import sys
4+
from typing import Any, Dict, List, NamedTuple, Optional, overload, TextIO, Tuple, Type, Union, ContextManager
5+
from types import ModuleType
6+
7+
if sys.version_info >= (3, 8):
8+
from typing import Literal
9+
else:
10+
from typing_extensions import Literal
511

612
@overload
713
def warn(message: str, category: Optional[Type[Warning]] = ..., stacklevel: int = ...) -> None: ...
@@ -37,10 +43,12 @@ class _Record(NamedTuple):
3743
file: Optional[TextIO]
3844
line: Optional[str]
3945

40-
class catch_warnings:
41-
def __init__(self, *, record: bool = ...,
42-
module: Optional[ModuleType] = ...) -> None: ...
43-
def __enter__(self) -> Optional[List[_Record]]: ...
44-
def __exit__(self, exc_type: Optional[Type[BaseException]],
45-
exc_val: Optional[BaseException],
46-
exc_tb: Optional[TracebackType]) -> None: ...
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]]]: ...

0 commit comments

Comments
 (0)