Skip to content

Commit 88c3673

Browse files
committed
Add warnings and test for them.
1 parent a2b9db2 commit 88c3673

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/napari_matplotlib/tests/test_util.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ def test_get_size_from_css(mocker):
2828
}
2929
"""
3030
mocker.patch("napari.qt.get_current_stylesheet").return_value = test_css
31-
assert from_css_get_size_of("Flibble", (1, 1)) == QSize(123, 456)
31+
assert from_css_get_size_of("Flibble", (1, 2)) == QSize(123, 456)
3232

3333

3434
def test_fallback_if_missing_dimensions(mocker):
3535
"""Test fallback if given something that doesn't have dimensions"""
3636
test_css = " Flobble { background-color: rgb(0, 97, 163); } "
3737
mocker.patch("napari.qt.get_current_stylesheet").return_value = test_css
38-
assert from_css_get_size_of("Flobble", (1, 1)) == QSize(1, 1)
38+
with pytest.warns(RuntimeWarning, match="Unable to find DimensionToken"):
39+
assert from_css_get_size_of("Flobble", (1, 2)) == QSize(1, 2)
3940

4041

4142
def test_fallback_if_prelude_not_in_css():
4243
"""Test fallback if given something not in the css"""
43-
assert from_css_get_size_of("AQButtonThatDoesntExist", (1, 1)) == QSize(
44-
1, 1
45-
)
44+
doesntexist = "AQButtonThatDoesntExist"
45+
with pytest.warns(RuntimeWarning, match=f"Unable to find {doesntexist}"):
46+
assert from_css_get_size_of(doesntexist, (1, 2)) == QSize(1, 2)

src/napari_matplotlib/util.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import List, Optional, Tuple, Union
2+
from warnings import warn
23

34
import napari.qt
45
import tinycss2
@@ -67,6 +68,7 @@ def _get_dimension(
6768
and name.value == id_name
6869
):
6970
return value.int_value
71+
warn(f"Unable to find DimensionToken for {id_name}", RuntimeWarning)
7072
return None
7173

7274

@@ -95,4 +97,9 @@ def from_css_get_size_of(
9597
h = _get_dimension(rule.content, "max-height")
9698
if w and h:
9799
return QSize(w, h)
100+
warn(
101+
f"Unable to find {qt_element_name} or unable to find its size in "
102+
f"the current Napari stylesheet, falling back to {fallback}",
103+
RuntimeWarning,
104+
)
98105
return QSize(*fallback)

0 commit comments

Comments
 (0)