File tree 2 files changed +21
-10
lines changed
2 files changed +21
-10
lines changed Original file line number Diff line number Diff line change @@ -218,7 +218,12 @@ def _assert_raised_with_correct_stacklevel(
218
218
frame = inspect .currentframe ()
219
219
for _ in range (4 ):
220
220
frame = frame .f_back # type: ignore[union-attr]
221
- caller_filename = inspect .getfile (frame ) # type: ignore[arg-type]
221
+ try :
222
+ caller_filename = inspect .getfile (frame ) # type: ignore[arg-type]
223
+ finally :
224
+ # See note in
225
+ # https://docs.python.org/3/library/inspect.html#inspect.Traceback
226
+ del frame
222
227
msg = (
223
228
"Warning not set with correct stacklevel. "
224
229
f"File where warning is raised: { actual_warning .filename } != "
Original file line number Diff line number Diff line change 9
9
10
10
if TYPE_CHECKING :
11
11
from collections .abc import Generator
12
+ from types import FrameType
12
13
13
14
14
15
@contextlib .contextmanager
@@ -42,15 +43,20 @@ def find_stack_level() -> int:
42
43
test_dir = os .path .join (pkg_dir , "tests" )
43
44
44
45
# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
45
- frame = inspect .currentframe ()
46
- n = 0
47
- while frame :
48
- fname = inspect .getfile (frame )
49
- if fname .startswith (pkg_dir ) and not fname .startswith (test_dir ):
50
- frame = frame .f_back
51
- n += 1
52
- else :
53
- break
46
+ frame : FrameType | None = inspect .currentframe ()
47
+ try :
48
+ n = 0
49
+ while frame :
50
+ filename = inspect .getfile (frame )
51
+ if filename .startswith (pkg_dir ) and not filename .startswith (test_dir ):
52
+ frame = frame .f_back
53
+ n += 1
54
+ else :
55
+ break
56
+ finally :
57
+ # See note in
58
+ # https://docs.python.org/3/library/inspect.html#inspect.Traceback
59
+ del frame
54
60
return n
55
61
56
62
You can’t perform that action at this time.
0 commit comments