Fix 'warns' type. 'expected_warning' can accept None as the value

This commit is contained in:
Maksim Beliaev 2022-02-07 11:59:33 +01:00
parent 18c0cfc0de
commit fb803a44b0
2 changed files with 12 additions and 3 deletions

View File

@ -201,6 +201,7 @@ Luke Murphy
Maciek Fijalkowski
Maho
Maik Figura
Maksim Beliaev
Mandeep Bhutani
Manuel Krebber
Marc Schlaich

View File

@ -84,7 +84,7 @@ def deprecated_call(
@overload
def warns(
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]] = ...,
expected_warning: Optional[Union[Type[Warning], Tuple[Type[Warning], ...]]] = ...,
*,
match: Optional[Union[str, Pattern[str]]] = ...,
) -> "WarningsChecker":
@ -93,7 +93,7 @@ def warns(
@overload
def warns(
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]],
expected_warning: Optional[Union[Type[Warning], Tuple[Type[Warning], ...]]],
func: Callable[..., T],
*args: Any,
**kwargs: Any,
@ -102,7 +102,7 @@ def warns(
def warns(
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]] = Warning,
expected_warning: Optional[Union[Type[Warning], Tuple[Type[Warning], ...]]] = Warning,
*args: Any,
match: Optional[Union[str, Pattern[str]]] = None,
**kwargs: Any,
@ -138,6 +138,14 @@ def warns(
...
Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
Recording warnings
To record with func:pytest.warns without asserting anything about the warnings,
pass None as the expected warning type:
>>> with pytest.warns(None) as record:
... warnings.warn("user", UserWarning)
... warnings.warn("runtime", RuntimeWarning)
>>> assert len(record) == 2
"""
__tracebackhide__ = True
if not args: