Skip to content

Commit 77e609d

Browse files
committed
Adjust stack level returned to account for it being calculated one level deeper than it is being used.
Also, make the stack level calculator private until there is a need to make it public.
1 parent 9d5d72f commit 77e609d

File tree

1 file changed

+11
-8
lines changed
  • python/ipywidgets/ipywidgets/widgets

1 file changed

+11
-8
lines changed

python/ipywidgets/ipywidgets/widgets/utils.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,28 @@
1111
# added in the process. For example, with the deprecation warning in the
1212
# __init__ below, the appropriate stacklevel will change depending on how deep
1313
# the inheritance hierarchy is.
14-
def external_stacklevel(internal):
15-
"""Find the first frame that doesn't contain any of the given internal strings
14+
def _external_stacklevel(internal):
15+
"""Find the stacklevel of the first frame that doesn't contain any of the given internal strings
1616
17-
The depth will be 2 at minimum in order to start checking at the caller of
18-
the function that called this utility method.
17+
The stacklevel will be from the perspective of the caller, and will start checking at the caller
18+
of the function that called this function. Thus, the minimum stacklevel will be 1.
1919
"""
20+
# Get the level of my caller's caller
2021
level = 2
2122
frame = sys._getframe(level)
2223
while frame and any(s in frame.f_code.co_filename for s in internal):
2324
level +=1
2425
frame = frame.f_back
25-
return level
26+
# the returned value will be used one level up from here, so subtract one
27+
return level-1
2628

2729
def deprecation(message, internal=None):
28-
"""Generate a deprecation warning targeting the first external frame
30+
"""Generate a deprecation warning targeting the first frame outside the ipywidgets library.
2931
3032
internal is a list of strings, which if they appear in filenames in the
31-
frames, the frames will also be considered internal.
33+
frames, the frames will also be considered internal. This can be useful if we know that ipywidgets
34+
is calling out to, for example, traitlets internally.
3235
"""
3336
if internal is None:
3437
internal = []
35-
warnings.warn(message, DeprecationWarning, stacklevel=external_stacklevel(internal+['ipywidgets/widgets/']))
38+
warnings.warn(message, DeprecationWarning, stacklevel=_external_stacklevel(internal+['ipywidgets/widgets/']))

0 commit comments

Comments
 (0)