add unittest.case._AssertRaisesBaseContext class (#11158)

This commit is contained in:
Stephen Morton
2023-12-18 14:07:47 -08:00
committed by GitHub
parent f8e738621f
commit ad51dde631
2 changed files with 22 additions and 6 deletions

View File

@@ -2,7 +2,7 @@ import logging
import sys
from types import TracebackType
from typing import ClassVar, Generic, NamedTuple, TypeVar
from unittest.case import TestCase
from unittest.case import TestCase, _BaseTestCaseContext
_L = TypeVar("_L", None, _LoggingWatcher)
@@ -10,9 +10,8 @@ class _LoggingWatcher(NamedTuple):
records: list[logging.LogRecord]
output: list[str]
class _AssertLogsContext(Generic[_L]):
class _AssertLogsContext(_BaseTestCaseContext, Generic[_L]):
LOGGING_FORMAT: ClassVar[str]
test_case: TestCase
logger_name: str
level: int
msg: None

View File

@@ -25,8 +25,26 @@ _P = ParamSpec("_P")
DIFF_OMITTED: str
class _BaseTestCaseContext:
test_case: TestCase
def __init__(self, test_case: TestCase) -> None: ...
class _AssertRaisesBaseContext(_BaseTestCaseContext):
expected: type[BaseException] | tuple[type[BaseException], ...]
expected_regex: Pattern[str] | None
obj_name: str | None
msg: str | None
def __init__(
self,
expected: type[BaseException] | tuple[type[BaseException], ...],
test_case: TestCase,
expected_regex: str | Pattern[str] | None = None,
) -> None: ...
# This returns Self if args is the empty list, and None otherwise.
# but it's not possible to construct an overload which expresses that
def handle(self, name: str, args: list[Any], kwargs: dict[str, Any]) -> Any: ...
if sys.version_info >= (3, 9):
from unittest._log import _AssertLogsContext, _LoggingWatcher
else:
@@ -41,7 +59,6 @@ else:
class _AssertLogsContext(_BaseTestCaseContext, Generic[_L]):
LOGGING_FORMAT: ClassVar[str]
test_case: TestCase
logger_name: str
level: int
msg: None
@@ -310,7 +327,7 @@ class FunctionTestCase(TestCase):
def __hash__(self) -> int: ...
def __eq__(self, other: object) -> bool: ...
class _AssertRaisesContext(Generic[_E]):
class _AssertRaisesContext(_AssertRaisesBaseContext, Generic[_E]):
exception: _E
def __enter__(self) -> Self: ...
def __exit__(
@@ -319,7 +336,7 @@ class _AssertRaisesContext(Generic[_E]):
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
class _AssertWarnsContext:
class _AssertWarnsContext(_AssertRaisesBaseContext):
warning: WarningMessage
filename: str
lineno: int