Skip to content

Commit d63d1a9

Browse files
committed
improve warning stack level + fix minor issue
1 parent a6c5bea commit d63d1a9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

noxfile.py

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def test_python_suite(session: Session) -> None:
187187
args = ["coverage", "run", "--source=src/idom", "--module", *args]
188188
install_idom_dev(session)
189189
else:
190+
args.remove("--no-cov")
190191
session.log("Coverage won't be checked")
191192
session.install(".[all]")
192193

src/idom/_option.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

33
import os
4-
import warnings
4+
from inspect import currentframe
55
from logging import getLogger
6-
from typing import Any, Callable, Generic, TypeVar, cast
6+
from types import FrameType
7+
from typing import Any, Callable, Generic, Iterator, TypeVar, cast
8+
from warnings import warn
79

810

911
_O = TypeVar("_O")
@@ -129,12 +131,32 @@ def __init__(self, new_opt: Option[_O], name: str) -> None:
129131

130132
@property # type: ignore
131133
def _current(self) -> _O:
132-
warnings.warn(
134+
warn(
133135
f"{self.name!r} has been renamed to {self._new_opt.name!r}",
134136
DeprecationWarning,
137+
stacklevel=_frame_depth_in_module() + 1,
135138
)
136139
return self._new_opt.current
137140

138141
@_current.setter
139142
def _current(self, new: _O) -> None:
140143
self._new_opt.current = new
144+
145+
146+
def _frame_depth_in_module() -> int:
147+
depth = 0
148+
for frame in _iter_frames(2):
149+
if frame.f_globals.get("__name__") != __name__:
150+
break
151+
depth += 1
152+
return depth
153+
154+
155+
def _iter_frames(index: int = 1) -> Iterator[FrameType]:
156+
frame = currentframe()
157+
while frame is not None:
158+
if index == 0:
159+
yield frame
160+
else:
161+
index -= 1
162+
frame = frame.f_back

0 commit comments

Comments
 (0)