Merge pull request #10660 from ikonst/2023-01-13-raises-typing

Derive pytest.raises from AbstractContextManager
This commit is contained in:
Ronny Pfannschmidt 2023-01-18 06:42:46 +01:00 committed by GitHub
commit 096b942ec4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

View File

@ -0,0 +1,2 @@
Fix :py:func:`pytest.raises` to return a 'ContextManager' so that type-checkers could narrow
:code:`pytest.raises(...) if ... else nullcontext()` down to 'ContextManager' rather than 'object'.

View File

@ -8,7 +8,7 @@ from types import TracebackType
from typing import Any from typing import Any
from typing import Callable from typing import Callable
from typing import cast from typing import cast
from typing import Generic from typing import ContextManager
from typing import List from typing import List
from typing import Mapping from typing import Mapping
from typing import Optional from typing import Optional
@ -957,7 +957,7 @@ raises.Exception = fail.Exception # type: ignore
@final @final
class RaisesContext(Generic[E]): class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]):
def __init__( def __init__(
self, self,
expected_exception: Union[Type[E], Tuple[Type[E], ...]], expected_exception: Union[Type[E], Tuple[Type[E], ...]],

View File

@ -3,6 +3,11 @@
This file is not executed, it is only checked by mypy to ensure that This file is not executed, it is only checked by mypy to ensure that
none of the code triggers any mypy errors. none of the code triggers any mypy errors.
""" """
import contextlib
from typing import Optional
from typing_extensions import assert_type
import pytest import pytest
@ -22,3 +27,9 @@ def check_fixture_ids_callable() -> None:
@pytest.mark.parametrize("func", [str, int], ids=lambda x: str(x.__name__)) @pytest.mark.parametrize("func", [str, int], ids=lambda x: str(x.__name__))
def check_parametrize_ids_callable(func) -> None: def check_parametrize_ids_callable(func) -> None:
pass pass
def check_raises_is_a_context_manager(val: bool) -> None:
with pytest.raises(RuntimeError) if val else contextlib.nullcontext() as excinfo:
pass
assert_type(excinfo, Optional[pytest.ExceptionInfo[RuntimeError]])