Add typestubs for the warnings module (#3543)

This commit is contained in:
dave-shawley
2019-12-21 15:29:34 -05:00
committed by Jelle Zijlstra
parent 0c563130fd
commit c44a556fb0
3 changed files with 74 additions and 34 deletions

View File

@@ -7,28 +7,58 @@ if sys.version_info >= (3, 0):
else:
default_action: str
once_registry: Dict[Any, Any]
filters: List[Tuple[Any, ...]]
@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: ...
if sys.version_info >= (3, 6):
@overload
def warn(message: str, category: Optional[Type[Warning]] = ..., stacklevel: int = ..., source: Optional[Any] = ...) -> None: ...
@overload
def warn(message: Warning, category: Any = ..., stacklevel: int = ..., source: Optional[Any] = ...) -> 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]] = ...,
source: Optional[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]] = ...,
source: Optional[Any] = ...,
) -> None: ...
else:
@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: ...

View File

@@ -1,4 +1,5 @@
from typing import List, NamedTuple, Optional, overload, TextIO, Type
import sys
from typing import Any, List, NamedTuple, Optional, overload, TextIO, Type
from types import ModuleType, TracebackType
from typing_extensions import Literal
@@ -14,13 +15,20 @@ def filterwarnings(
def simplefilter(action: str, category: Type[Warning] = ..., lineno: int = ..., append: bool = ...) -> None: ...
def resetwarnings() -> None: ...
class _Record(NamedTuple):
message: str
class _OptionError(Exception): ...
class WarningMessage:
message: Warning
category: Type[Warning]
filename: str
lineno: int
file: Optional[TextIO]
line: Optional[str]
filename: Any
lineno: Any
file: Optional[Any]
line: Optional[Any]
if sys.version_info >= (3, 6):
source: Optional[Any]
def __init__(self, message: Warning, category: Type[Warning], filename: Any, lineno: Any, file: Optional[Any] = ..., line: Optional[Any] = ..., source: Optional[Any] = ...) -> None: ...
else:
def __init__(self, message: Warning, category: Type[Warning], filename: Any, lineno: Any, file: Optional[Any] = ..., line: Optional[Any] = ...) -> None: ...
class catch_warnings:
@overload
@@ -29,7 +37,7 @@ class catch_warnings:
def __new__(cls, *, record: Literal[True], module: Optional[ModuleType] = ...) -> _catch_warnings_with_records: ...
@overload
def __new__(cls, *, record: bool, module: Optional[ModuleType] = ...) -> catch_warnings: ...
def __enter__(self) -> Optional[List[_Record]]: ...
def __enter__(self) -> Optional[List[WarningMessage]]: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
@@ -38,4 +46,4 @@ class _catch_warnings_without_records(catch_warnings):
def __enter__(self) -> None: ...
class _catch_warnings_with_records(catch_warnings):
def __enter__(self) -> List[_Record]: ...
def __enter__(self) -> List[WarningMessage]: ...

View File

@@ -2,6 +2,7 @@ import datetime
import logging
import sys
import unittest.result
import warnings
from types import TracebackType
from typing import (
Any, AnyStr, Callable, Container, ContextManager, Dict, FrozenSet, Generic,
@@ -233,9 +234,10 @@ class _AssertRaisesContext(Generic[_E]):
exc_tb: Optional[TracebackType]) -> bool: ...
class _AssertWarnsContext:
warning: Warning
warning: warnings.WarningMessage
filename: str
lineno: int
warnings: List[warnings.WarningMessage]
def __enter__(self) -> _AssertWarnsContext: ...
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> None: ...