Skip to content

Commit 5784965

Browse files
committed
Move _AssertRaisesContext
This allows us to avoid the quoting due to a forward reference. We also drop some unnecessary `type: ignore` comments. This is done in preparation of some typing bug fixes. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent c0d384c commit 5784965

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

testtools/testcase.py

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,61 @@ def unique_text_generator(prefix: str) -> Iterator[str]:
243243
index = index + 1
244244

245245

246+
class _AssertRaisesContext(Generic[_E]):
247+
"""A context manager to handle expected exceptions for assertRaises.
248+
249+
This provides compatibility with unittest's assertRaises context manager.
250+
"""
251+
252+
def __init__(
253+
self,
254+
expected: type[_E] | tuple[type[BaseException], ...],
255+
test_case: "TestCase",
256+
msg: str | None = None,
257+
) -> None:
258+
"""Construct an `_AssertRaisesContext`.
259+
260+
:param expected: The type of exception to expect.
261+
:param test_case: The TestCase instance using this context.
262+
:param msg: An optional message explaining the failure.
263+
"""
264+
self.expected = expected
265+
self.test_case = test_case
266+
self.msg = msg
267+
self.exception: _E | None = None
268+
269+
def __enter__(self) -> "Self":
270+
return self
271+
272+
def __exit__(
273+
self,
274+
exc_type: type[BaseException] | None,
275+
exc_value: BaseException | None,
276+
traceback: types.TracebackType | None,
277+
) -> bool:
278+
if exc_type is None:
279+
try:
280+
if isinstance(self.expected, tuple):
281+
exc_name = "({})".format(
282+
", ".join(e.__name__ for e in self.expected)
283+
)
284+
else:
285+
exc_name = self.expected.__name__
286+
except AttributeError:
287+
exc_name = str(self.expected)
288+
if self.msg:
289+
error_msg = f"{exc_name} not raised : {self.msg}"
290+
else:
291+
error_msg = f"{exc_name} not raised"
292+
raise self.test_case.failureException(error_msg)
293+
if not issubclass(exc_type, self.expected):
294+
# let unexpected exceptions pass through
295+
return False
296+
# store exception for later retrieval
297+
self.exception = cast(_E, exc_value)
298+
return True
299+
300+
246301
class TestCase(unittest.TestCase):
247302
"""Extensions to the basic TestCase.
248303
@@ -513,41 +568,41 @@ def assertRaises(
513568
**kwargs: _P.kwargs,
514569
) -> BaseException: ...
515570

516-
@overload # type: ignore[override]
571+
@overload
517572
def assertRaises(
518573
self,
519574
expected_exception: type[_E],
520575
callable: None = ...,
521-
) -> "_AssertRaisesContext[_E]": ...
576+
) -> _AssertRaisesContext[_E]: ...
522577

523-
@overload # type: ignore[override]
578+
@overload
524579
def assertRaises(
525580
self,
526581
expected_exception: tuple[type[_E], type[_E2]],
527582
callable: None = ...,
528-
) -> "_AssertRaisesContext[_E | _E2]": ...
583+
) -> _AssertRaisesContext[_E | _E2]: ...
529584

530-
@overload # type: ignore[override]
585+
@overload
531586
def assertRaises(
532587
self,
533588
expected_exception: tuple[type[_E], type[_E2], type[_E3]],
534589
callable: None = ...,
535-
) -> "_AssertRaisesContext[_E | _E2 | _E3]": ...
590+
) -> _AssertRaisesContext[_E | _E2 | _E3]: ...
536591

537-
@overload # type: ignore[override]
592+
@overload
538593
def assertRaises(
539594
self,
540595
expected_exception: tuple[type[BaseException], ...],
541596
callable: None = ...,
542-
) -> "_AssertRaisesContext[BaseException]": ...
597+
) -> _AssertRaisesContext[BaseException]: ...
543598

544-
def assertRaises( # type: ignore[override, misc]
599+
def assertRaises( # type: ignore[misc]
545600
self,
546601
expected_exception: type[BaseException] | tuple[type[BaseException], ...],
547602
callable: Callable[_P, _R] | None = None,
548603
*args: _P.args,
549604
**kwargs: _P.kwargs,
550-
) -> "_AssertRaisesContext[BaseException] | BaseException":
605+
) -> _AssertRaisesContext[BaseException] | BaseException:
551606
"""Fail unless an exception of class expected_exception is thrown
552607
by callable when invoked with arguments args and keyword
553608
arguments kwargs. If a different type of exception is
@@ -1247,61 +1302,6 @@ def _id(obj: _F) -> _F:
12471302
return _id
12481303

12491304

1250-
class _AssertRaisesContext(Generic[_E]):
1251-
"""A context manager to handle expected exceptions for assertRaises.
1252-
1253-
This provides compatibility with unittest's assertRaises context manager.
1254-
"""
1255-
1256-
def __init__(
1257-
self,
1258-
expected: type[_E] | tuple[type[BaseException], ...],
1259-
test_case: TestCase,
1260-
msg: str | None = None,
1261-
) -> None:
1262-
"""Construct an `_AssertRaisesContext`.
1263-
1264-
:param expected: The type of exception to expect.
1265-
:param test_case: The TestCase instance using this context.
1266-
:param msg: An optional message explaining the failure.
1267-
"""
1268-
self.expected = expected
1269-
self.test_case = test_case
1270-
self.msg = msg
1271-
self.exception: _E | None = None
1272-
1273-
def __enter__(self) -> "Self":
1274-
return self
1275-
1276-
def __exit__(
1277-
self,
1278-
exc_type: type[BaseException] | None,
1279-
exc_value: BaseException | None,
1280-
traceback: types.TracebackType | None,
1281-
) -> bool:
1282-
if exc_type is None:
1283-
try:
1284-
if isinstance(self.expected, tuple):
1285-
exc_name = "({})".format(
1286-
", ".join(e.__name__ for e in self.expected)
1287-
)
1288-
else:
1289-
exc_name = self.expected.__name__
1290-
except AttributeError:
1291-
exc_name = str(self.expected)
1292-
if self.msg:
1293-
error_msg = f"{exc_name} not raised : {self.msg}"
1294-
else:
1295-
error_msg = f"{exc_name} not raised"
1296-
raise self.test_case.failureException(error_msg)
1297-
if not issubclass(exc_type, self.expected):
1298-
# let unexpected exceptions pass through
1299-
return False
1300-
# store exception for later retrieval
1301-
self.exception = cast(_E, exc_value)
1302-
return True
1303-
1304-
13051305
class ExpectedException:
13061306
"""A context manager to handle expected exceptions.
13071307

0 commit comments

Comments
 (0)