Skip to content

Commit fed14cb

Browse files
committed
Add AssertRaisesContext.exc_val
1 parent c19c27a commit fed14cb

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ with assert_raises(KeyError) as context:
1919
...
2020
```
2121

22+
* Add `AssertRaisesContext.exc_val` property to access the caught
23+
exception after leaving the context manager:
24+
25+
```python
26+
with assert_raises(KeyError) as context:
27+
...
28+
assert_equal("expected message", str(context.exc_val))
29+
```
30+
2231
News in asserts 0.9.1
2332
=====================
2433

asserts/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,16 +880,18 @@ def __init__(self, exception, msg_fmt="{msg}"):
880880
self.exception = exception
881881
self.msg_fmt = msg_fmt
882882
self._exc_type = exception
883+
self._exc_val = None
883884
self._exception_name = getattr(exception, "__name__", str(exception))
884885
self._tests = []
885886

886887
def __enter__(self):
887888
return self
888889

889890
def __exit__(self, exc_type, exc_val, exc_tb):
890-
if not exc_type:
891+
if not exc_type or not exc_val:
891892
msg = "{} not raised".format(self._exception_name)
892893
fail(self.format_message(msg))
894+
self._exc_val = exc_val
893895
if not issubclass(exc_type, self.exception):
894896
return False
895897
for test in self._tests:
@@ -913,6 +915,12 @@ class was raised. The callback will get the raised exception as only
913915
"""
914916
self._tests.append(cb)
915917

918+
@property
919+
def exc_val(self):
920+
if self._exc_val is None:
921+
raise RuntimeError("must be called after leaving the context")
922+
return self._exc_val
923+
916924

917925
class AssertRaisesRegexContext(AssertRaisesContext):
918926

asserts/__init__.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class AssertRaisesContext(Generic[_E]):
3131
) -> bool: ...
3232
def format_message(self, default_msg: Text) -> Text: ...
3333
def add_test(self, cb: Callable[[_E], Any]) -> None: ...
34+
@property
35+
def exc_val(self) -> _E: ...
3436

3537
class AssertRaisesErrnoContext(AssertRaisesContext[_E]):
3638
expected_errno: int

test_asserts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,17 @@ def test_assert_raises__raises_right_exception(self):
936936
with assert_raises(KeyError):
937937
raise KeyError()
938938

939+
def test_assert_raises__exc_val(self):
940+
exc = KeyError()
941+
with assert_raises(KeyError) as context:
942+
raise exc
943+
assert_is(exc, context.exc_val)
944+
945+
def test_assert_raises__exc_val_within_context(self):
946+
with assert_raises(RuntimeError):
947+
with assert_raises(KeyError) as context:
948+
context.exc_val
949+
939950
def test_assert_raises__raises_subclass(self):
940951
class MyError(IndexError):
941952
pass

0 commit comments

Comments
 (0)