Fix typing of expect_failure()

TODO: put _XfailMarkDecorator fix into its own pull request
TODO: squash into previous commit
This commit is contained in:
Manuel Jacob 2022-07-01 20:47:45 +02:00
parent 8c4ca32f8d
commit d510bbe832
2 changed files with 19 additions and 12 deletions

View File

@ -426,7 +426,9 @@ if TYPE_CHECKING:
*conditions: Union[str, bool],
reason: str = ...,
run: bool = ...,
raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = ...,
raises: Optional[
Union[Type[BaseException], Tuple[Type[BaseException], ...]]
] = ...,
strict: bool = ...,
) -> MarkDecorator:
...

View File

@ -6,6 +6,7 @@ import traceback
from collections.abc import Mapping
from contextvars import ContextVar
from typing import Generator
from typing import MutableMapping
from typing import Optional
from typing import Tuple
from typing import Type
@ -306,19 +307,23 @@ def pytest_runtest_protocol(item: Item, nextitem: Optional[Item]) -> None:
current_item_var.set(item)
_not_passed = object()
class _NotPassed:
pass
_not_passed = _NotPassed()
def expect_failure(
reason: str = _not_passed,
raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = _not_passed,
strict: bool = _not_passed,
reason: str = "",
raises: Optional[
Union[Type[BaseException], Tuple[Type[BaseException], ...]]
] = None,
strict: Union[bool, _NotPassed] = _not_passed,
) -> None:
kwargs = {}
if reason is not _not_passed:
kwargs["reason"] = reason
if raises is not _not_passed:
kwargs["raises"] = raises
if strict is not _not_passed:
kwargs: MutableMapping[str, bool] = {}
if not isinstance(strict, _NotPassed):
kwargs["strict"] = strict
current_item_var.get().add_marker(MARK_GEN.xfail(**kwargs))
current_item_var.get().add_marker(
MARK_GEN.xfail(reason=reason, raises=raises, **kwargs)
)