From c44a556fb00f6225f1b4209e24dac9e54ae990aa Mon Sep 17 00:00:00 2001 From: dave-shawley Date: Sat, 21 Dec 2019 15:29:34 -0500 Subject: [PATCH] Add typestubs for the warnings module (#3543) --- stdlib/2and3/_warnings.pyi | 78 ++++++++++++++++++++++++++------------ stdlib/2and3/warnings.pyi | 26 ++++++++----- stdlib/3/unittest/case.pyi | 4 +- 3 files changed, 74 insertions(+), 34 deletions(-) diff --git a/stdlib/2and3/_warnings.pyi b/stdlib/2and3/_warnings.pyi index f764f7c77..6c8906e09 100644 --- a/stdlib/2and3/_warnings.pyi +++ b/stdlib/2and3/_warnings.pyi @@ -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: ... diff --git a/stdlib/2and3/warnings.pyi b/stdlib/2and3/warnings.pyi index 71be4895d..87033aafd 100644 --- a/stdlib/2and3/warnings.pyi +++ b/stdlib/2and3/warnings.pyi @@ -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]: ... diff --git a/stdlib/3/unittest/case.pyi b/stdlib/3/unittest/case.pyi index 3a17a3a9a..01ae808f2 100644 --- a/stdlib/3/unittest/case.pyi +++ b/stdlib/3/unittest/case.pyi @@ -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: ...