Skip to content

Commit fa17b1d

Browse files
committed
MAINT: Refactor _AssertRaisesContextManager
Rewrite _AssertRaisesContextManager with more documentation and remove vestigial assertRaises. Follow-up to pandas-devgh-16089.
1 parent c847884 commit fa17b1d

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

pandas/tests/test_base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class CheckImmutable(object):
4545
mutable_regex = re.compile('does not support mutable operations')
4646

4747
def check_mutable_error(self, *args, **kwargs):
48-
# pass whatever functions you normally would to assertRaises (after the
49-
# Exception kind)
48+
# Pass whatever function you normally would to assertRaisesRegexp
49+
# (after the Exception kind).
5050
tm.assertRaisesRegexp(TypeError, self.mutable_regex, *args, **kwargs)
5151

5252
def test_no_mutable_funcs(self):
@@ -70,6 +70,7 @@ def delslice():
7070

7171
self.check_mutable_error(delslice)
7272
mutable_methods = getattr(self, "mutable_methods", [])
73+
7374
for meth in mutable_methods:
7475
self.check_mutable_error(getattr(self.container, meth))
7576

pandas/util/testing.py

+61-22
Original file line numberDiff line numberDiff line change
@@ -2500,40 +2500,79 @@ def assertRaisesRegexp(_exception, _regexp, _callable=None, *args, **kwargs):
25002500

25012501
class _AssertRaisesContextmanager(object):
25022502
"""
2503-
Handles the behind the scenes work
2504-
for assertRaises and assertRaisesRegexp
2503+
Context manager behind assertRaisesRegexp.
25052504
"""
25062505

2507-
def __init__(self, exception, regexp=None, *args, **kwargs):
2506+
def __init__(self, exception, regexp=None):
2507+
"""
2508+
Initialize an _AssertRaisesContextManager instance.
2509+
2510+
Parameters
2511+
----------
2512+
exception : class
2513+
The expected Exception class.
2514+
regexp : str, default None
2515+
The regex to compare against the Exception message.
2516+
"""
2517+
25082518
self.exception = exception
2519+
25092520
if regexp is not None and not hasattr(regexp, "search"):
25102521
regexp = re.compile(regexp, re.DOTALL)
2522+
25112523
self.regexp = regexp
25122524

25132525
def __enter__(self):
25142526
return self
25152527

2516-
def __exit__(self, exc_type, exc_value, traceback):
2528+
def __exit__(self, exc_type, exc_value, trace_back):
25172529
expected = self.exception
2518-
if not exc_type:
2519-
name = getattr(expected, "__name__", str(expected))
2520-
raise AssertionError("{0} not raised.".format(name))
2521-
if issubclass(exc_type, expected):
2522-
return self.handle_success(exc_type, exc_value, traceback)
2523-
return self.handle_failure(exc_type, exc_value, traceback)
2524-
2525-
def handle_failure(*args, **kwargs):
2526-
# Failed, so allow Exception to bubble up
2527-
return False
25282530

2529-
def handle_success(self, exc_type, exc_value, traceback):
2530-
if self.regexp is not None:
2531-
val = str(exc_value)
2532-
if not self.regexp.search(val):
2533-
e = AssertionError('"%s" does not match "%s"' %
2534-
(self.regexp.pattern, str(val)))
2535-
raise_with_traceback(e, traceback)
2536-
return True
2531+
if not exc_type:
2532+
exp_name = getattr(expected, "__name__", str(expected))
2533+
raise AssertionError("{0} not raised.".format(exp_name))
2534+
2535+
return self.exception_matches(exc_type, exc_value, trace_back)
2536+
2537+
def exception_matches(self, exc_type, exc_value, trace_back):
2538+
"""
2539+
Check that the Exception raised matches the expected Exception
2540+
and expected error message regular expression.
2541+
2542+
Parameters
2543+
----------
2544+
exc_type : class
2545+
The type of Exception raised.
2546+
exc_value : Exception
2547+
The instance of `exc_type` raised.
2548+
trace_back : stack trace object
2549+
The traceback object associated with `exc_value`.
2550+
2551+
Returns
2552+
-------
2553+
is_matched : bool
2554+
Whether or not the Exception raised matches the expected
2555+
Exception class and expected error message regular expression.
2556+
2557+
Raises
2558+
------
2559+
AssertionError : The error message provided does not match
2560+
the expected error message regular expression.
2561+
"""
2562+
2563+
if issubclass(exc_type, self.exception):
2564+
if self.regexp is not None:
2565+
val = str(exc_value)
2566+
2567+
if not self.regexp.search(val):
2568+
e = AssertionError('"%s" does not match "%s"' %
2569+
(self.regexp.pattern, str(val)))
2570+
raise_with_traceback(e, trace_back)
2571+
2572+
return True
2573+
else:
2574+
# Failed, so allow Exception to bubble up.
2575+
return False
25372576

25382577

25392578
@contextmanager

0 commit comments

Comments
 (0)