recwarn: improve return type annotation of non-contextmanager pytest.warns

It returns the return value of the function.
This commit is contained in:
Ran Benita 2020-06-25 14:08:47 +03:00
parent f00bec2a12
commit 4d813fdf5e
2 changed files with 9 additions and 5 deletions

View File

@ -9,6 +9,7 @@ from typing import List
from typing import Optional from typing import Optional
from typing import Pattern from typing import Pattern
from typing import Tuple from typing import Tuple
from typing import TypeVar
from typing import Union from typing import Union
from _pytest.compat import overload from _pytest.compat import overload
@ -20,6 +21,9 @@ if TYPE_CHECKING:
from typing import Type from typing import Type
T = TypeVar("T")
@fixture @fixture
def recwarn(): def recwarn():
"""Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. """Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.
@ -67,11 +71,10 @@ def warns(
@overload # noqa: F811 @overload # noqa: F811
def warns( # noqa: F811 def warns( # noqa: F811
expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]],
func: Callable, func: Callable[..., T],
*args: Any, *args: Any,
match: Optional[Union[str, "Pattern"]] = ...,
**kwargs: Any **kwargs: Any
) -> Union[Any]: ) -> T:
raise NotImplementedError() raise NotImplementedError()
@ -97,7 +100,7 @@ def warns( # noqa: F811
... warnings.warn("my warning", RuntimeWarning) ... warnings.warn("my warning", RuntimeWarning)
In the context manager form you may use the keyword argument ``match`` to assert In the context manager form you may use the keyword argument ``match`` to assert
that the exception matches a text or regex:: that the warning matches a text or regex::
>>> with warns(UserWarning, match='must be 0 or None'): >>> with warns(UserWarning, match='must be 0 or None'):
... warnings.warn("value must be 0 or None", UserWarning) ... warnings.warn("value must be 0 or None", UserWarning)

View File

@ -370,13 +370,14 @@ class TestWarns:
@pytest.mark.filterwarnings("ignore") @pytest.mark.filterwarnings("ignore")
def test_can_capture_previously_warned(self) -> None: def test_can_capture_previously_warned(self) -> None:
def f(): def f() -> int:
warnings.warn(UserWarning("ohai")) warnings.warn(UserWarning("ohai"))
return 10 return 10
assert f() == 10 assert f() == 10
assert pytest.warns(UserWarning, f) == 10 assert pytest.warns(UserWarning, f) == 10
assert pytest.warns(UserWarning, f) == 10 assert pytest.warns(UserWarning, f) == 10
assert pytest.warns(UserWarning, f) != "10" # type: ignore[comparison-overlap]
def test_warns_context_manager_with_kwargs(self) -> None: def test_warns_context_manager_with_kwargs(self) -> None:
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo: