Skip to content

Commit 1dc619a

Browse files
TST: check stacklevel in assert_produces_warning (GH9584)
1 parent a928f8d commit 1dc619a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pandas/util/testing.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,8 @@ def handle_success(self, exc_type, exc_value, traceback):
19181918

19191919

19201920
@contextmanager
1921-
def assert_produces_warning(expected_warning=Warning, filter_level="always", clear=None):
1921+
def assert_produces_warning(expected_warning=Warning, filter_level="always",
1922+
clear=None, check_stacklevel=True):
19221923
"""
19231924
Context manager for running code that expects to raise (or not raise)
19241925
warnings. Checks that code raises the expected warning and only the
@@ -1966,6 +1967,15 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always", cle
19661967
if (expected_warning and issubclass(actual_warning.category,
19671968
expected_warning)):
19681969
saw_warning = True
1970+
1971+
if check_stacklevel:
1972+
from inspect import getframeinfo, stack
1973+
caller = getframeinfo(stack()[2][0])
1974+
msg = ("Warning not set with correct stacklevel. File were warning"
1975+
" is raised: {0} != {1}. Warning message: {2}".format(
1976+
actual_warning.filename, caller.filename,
1977+
actual_warning.message))
1978+
assert actual_warning.filename == caller.filename, msg
19691979
else:
19701980
extra_warnings.append(actual_warning.category.__name__)
19711981
if expected_warning:

0 commit comments

Comments
 (0)