Skip to content

Commit c9c58ae

Browse files
authored
PERF: assert_produces_warning (#56009)
* faster assert_produces_warning * mypy
1 parent 4fd56b8 commit c9c58ae

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pandas/_testing/_warnings.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
contextmanager,
55
nullcontext,
66
)
7+
import inspect
78
import re
89
import sys
910
from typing import (
@@ -206,15 +207,14 @@ def _is_unexpected_warning(
206207
def _assert_raised_with_correct_stacklevel(
207208
actual_warning: warnings.WarningMessage,
208209
) -> None:
209-
from inspect import (
210-
getframeinfo,
211-
stack,
212-
)
213-
214-
caller = getframeinfo(stack()[4][0])
210+
# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
211+
frame = inspect.currentframe()
212+
for _ in range(4):
213+
frame = frame.f_back # type: ignore[union-attr]
214+
caller_filename = inspect.getfile(frame) # type: ignore[arg-type]
215215
msg = (
216216
"Warning not set with correct stacklevel. "
217217
f"File where warning is raised: {actual_warning.filename} != "
218-
f"{caller.filename}. Warning message: {actual_warning.message}"
218+
f"{caller_filename}. Warning message: {actual_warning.message}"
219219
)
220-
assert actual_warning.filename == caller.filename, msg
220+
assert actual_warning.filename == caller_filename, msg

0 commit comments

Comments
 (0)