Skip to content

Commit

Permalink
improve warning stack level + fix minor issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Jan 9, 2023
1 parent a6c5bea commit e3affd9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def test_python_suite(session: Session) -> None:
args = ["coverage", "run", "--source=src/idom", "--module", *args]
install_idom_dev(session)
else:
args.remove("--no-cov")
session.log("Coverage won't be checked")
session.install(".[all]")

Expand Down
28 changes: 25 additions & 3 deletions src/idom/_option.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import os
import warnings
from inspect import currentframe
from logging import getLogger
from typing import Any, Callable, Generic, TypeVar, cast
from types import FrameType
from typing import Any, Callable, Generic, Iterator, TypeVar, cast
from warnings import warn


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

@property # type: ignore
def _current(self) -> _O:
warnings.warn(
warn(
f"{self.name!r} has been renamed to {self._new_opt.name!r}",
DeprecationWarning,
stacklevel=_frame_depth_in_module() + 1,
)
return self._new_opt.current

@_current.setter
def _current(self, new: _O) -> None:
self._new_opt.current = new


def _frame_depth_in_module() -> int:
depth = -1 # use -1 to ignore current frame
for frame in _iter_frames():
if frame.f_globals.get("__name__") != __name__:
break
depth += 1
return depth


def _iter_frames() -> Iterator[FrameType]:
frame = currentframe()
if frame is None:
return # pragma: no cover
frame = frame.f_back # ignore current frame
while frame is not None:
yield frame
frame = frame.f_back

0 comments on commit e3affd9

Please sign in to comment.