|
11 | 11 | # added in the process. For example, with the deprecation warning in the
|
12 | 12 | # __init__ below, the appropriate stacklevel will change depending on how deep
|
13 | 13 | # the inheritance hierarchy is.
|
14 |
| -def external_stacklevel(internal): |
| 14 | +def _external_stacklevel(internal): |
15 | 15 | """Find the first frame that doesn't any of the given internal strings
|
16 | 16 |
|
17 |
| - The depth will be 2 at minimum in order to start checking at the caller of |
| 17 | + The depth will be 1 at minimum in order to start checking at the caller of |
18 | 18 | the function that called this utility method.
|
19 | 19 | """
|
| 20 | + # Get the level of my caller's caller |
20 | 21 | level = 2
|
21 | 22 | frame = sys._getframe(level)
|
22 | 23 | while frame and any(s in frame.f_code.co_filename for s in internal):
|
23 | 24 | level +=1
|
24 | 25 | 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 |
26 | 28 |
|
27 | 29 | 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. |
29 | 31 |
|
30 | 32 | 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. |
32 | 35 | """
|
33 | 36 | if internal is None:
|
34 | 37 | 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