File tree 1 file changed +8
-8
lines changed 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change 4
4
contextmanager ,
5
5
nullcontext ,
6
6
)
7
+ import inspect
7
8
import re
8
9
import sys
9
10
from typing import (
@@ -206,15 +207,14 @@ def _is_unexpected_warning(
206
207
def _assert_raised_with_correct_stacklevel (
207
208
actual_warning : warnings .WarningMessage ,
208
209
) -> 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]
215
215
msg = (
216
216
"Warning not set with correct stacklevel. "
217
217
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 } "
219
219
)
220
- assert actual_warning .filename == caller . filename , msg
220
+ assert actual_warning .filename == caller_filename , msg
You can’t perform that action at this time.
0 commit comments