Skip to content

Commit 0f5268b

Browse files
committed
Merge branch 'stacklevel' of github.com:jasongrout/ipywidgets into stacklevel
2 parents 7d237c6 + 28a8b9d commit 0f5268b

File tree

5 files changed

+103
-23
lines changed

5 files changed

+103
-23
lines changed
Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,72 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
import inspect
45
import pytest
56

67
from ..utils import deprecation
8+
from .utils import call_method
9+
10+
PYTEST_PATH = inspect.getfile(pytest.Function)
11+
CALL_PATH = inspect.getfile(call_method)
712

813
def test_deprecation():
9-
with pytest.deprecated_call() as w:
14+
with pytest.deprecated_call() as record:
1015
deprecation('Deprecated call')
11-
# Make sure the deprecation pointed to the external function calling this test function
12-
assert w[0].filename.endswith('_pytest/python.py')
16+
# Make sure the deprecation pointed to the external function calling this test function
17+
assert len(record) == 1
18+
assert record[0].filename == PYTEST_PATH
1319

14-
with pytest.deprecated_call() as w:
20+
with pytest.deprecated_call() as record:
1521
deprecation('Deprecated call', ['ipywidgets/widgets/tests'])
16-
# Make sure the deprecation pointed to the external function calling this test function
17-
assert w[0].filename.endswith('_pytest/python.py')
22+
# Make sure the deprecation pointed to the external function calling this test function
23+
assert len(record) == 1
24+
assert record[0].filename == PYTEST_PATH
1825

19-
with pytest.deprecated_call() as w:
26+
with pytest.deprecated_call() as record:
2027
deprecation('Deprecated call', 'ipywidgets/widgets/tests')
21-
# Make sure the deprecation pointed to the external function calling this test function
22-
assert w[0].filename.endswith('_pytest/python.py')
28+
# Make sure the deprecation pointed to the external function calling this test function
29+
assert len(record) == 1
30+
assert record[0].filename == PYTEST_PATH
31+
32+
with pytest.deprecated_call() as record:
33+
deprecation('Deprecated call', [])
34+
# Make sure the deprecation pointed to *this* file
35+
assert len(record) == 1
36+
assert record[0].filename == __file__
37+
38+
def test_deprecation_indirect():
39+
# If the line that calls "deprecation" is not internal, it is considered the source:
40+
with pytest.warns(DeprecationWarning) as record:
41+
call_method(deprecation, "test message", [])
42+
assert len(record) == 1
43+
assert record[0].filename == CALL_PATH
44+
45+
def test_deprecation_indirect_internal():
46+
# If the line that calls "deprecation" is internal, it is not considered the source:
47+
with pytest.warns(DeprecationWarning) as record:
48+
call_method(deprecation, "test message", [CALL_PATH])
49+
assert len(record) == 1
50+
assert record[0].filename == __file__
51+
52+
def test_deprecation_nested1():
53+
def level1():
54+
deprecation("test message", [])
55+
56+
with pytest.warns(DeprecationWarning) as record:
57+
call_method(level1)
58+
59+
assert len(record) == 1
60+
assert record[0].filename == __file__
61+
62+
def test_deprecation_nested2():
63+
def level2():
64+
deprecation("test message", [])
65+
def level1():
66+
level2()
67+
68+
with pytest.warns(DeprecationWarning) as record:
69+
call_method(level1)
70+
71+
assert len(record) == 1
72+
assert record[0].filename == __file__
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
import inspect
45
import pytest
56
from ipywidgets import Button
67

8+
PYTEST_PATH = inspect.getfile(pytest.Function)
9+
710
def test_deprecation_fa_icons():
8-
with pytest.deprecated_call():
11+
with pytest.deprecated_call() as record:
912
Button(icon='fa-home')
13+
assert len(record) == 1
14+
assert record[0].filename == PYTEST_PATH

python/ipywidgets/ipywidgets/widgets/tests/test_widget_string.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
from ..widget_string import Combobox, Text
4+
import inspect
55
import pytest
66

7+
from ..widget_string import Combobox, Text
8+
9+
10+
PYTEST_PATH = inspect.getfile(pytest.Function)
11+
12+
713

814
def test_combobox_creation_blank():
915
w = Combobox()
@@ -34,18 +40,30 @@ def test_combobox_creation_kwargs():
3440
)
3541
assert w.ensure_option == True
3642

37-
def test_deprecation_description_tooltip():
38-
with pytest.deprecated_call():
39-
Text(description_tooltip='tooltip')
40-
41-
with pytest.deprecated_call():
42-
Text().description_tooltip
43+
def test_tooltip_deprecation():
44+
with pytest.deprecated_call() as record:
45+
w = Text(description_tooltip="testing")
46+
assert len(record) == 1
47+
assert record[0].filename == PYTEST_PATH
4348

49+
with pytest.deprecated_call() as record:
50+
w.description_tooltip
51+
assert len(record) == 1
52+
assert record[0].filename == PYTEST_PATH
4453

45-
with pytest.deprecated_call():
46-
Text().description_tooltip = 'tooltip'
54+
with pytest.deprecated_call() as record:
55+
w.description_tooltip == "testing"
56+
assert len(record) == 1
57+
assert record[0].filename == PYTEST_PATH
4758

59+
with pytest.deprecated_call() as record:
60+
w.description_tooltip = "second value"
61+
assert len(record) == 1
62+
assert record[0].filename == PYTEST_PATH
63+
assert w.tooltip == "second value"
4864

49-
def test_deprecation_on_submit():
50-
with pytest.deprecated_call():
65+
def test_on_submit_deprecation():
66+
with pytest.deprecated_call() as record:
5167
Text().on_submit(lambda *args: ...)
68+
assert len(record) == 1
69+
assert record[0].filename == PYTEST_PATH

python/ipywidgets/ipywidgets/widgets/tests/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ def setup():
4545

4646
def teardown():
4747
teardown_test_comm()
48+
49+
def call_method(method, *args, **kwargs):
50+
method(*args, **kwargs)

python/ipywidgets/ipywidgets/widgets/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
from pathlib import Path
45
import sys
56
import inspect
67
import warnings
@@ -13,7 +14,7 @@
1314
# __init__ below, the appropriate stacklevel will change depending on how deep
1415
# the inheritance hierarchy is.
1516
def _external_stacklevel(internal):
16-
"""Find the first frame that doesn't any of the given internal strings
17+
"""Find the stacklevel of the first frame that doesn't contain any of the given internal strings
1718
1819
The depth will be 1 at minimum in order to start checking at the caller of
1920
the function that called this utility method.
@@ -28,8 +29,11 @@ def _external_stacklevel(internal):
2829
else:
2930
frame = inspect.stack(context=0)[level].frame
3031

32+
# Normalize the path separators:
33+
normalized_internal = [str(Path(s)) for s in internal]
34+
3135
# climb the stack frames while we see internal frames
32-
while frame and any(s in frame.f_code.co_filename for s in internal):
36+
while frame and any(s in str(Path(frame.f_code.co_filename)) for s in normalized_internal):
3337
level +=1
3438
frame = frame.f_back
3539

0 commit comments

Comments
 (0)