Skip to content

Commit 0968434

Browse files
authored
Merge pull request #5080 from plotly/fix-bogus-deprecationwarning
Suppress `DeprecationWarning` when updating `template.data`
2 parents 5fe330d + 2a0241d commit 0968434

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

plotly/graph_objs/layout/_template.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from plotly.basedatatypes import BaseLayoutHierarchyType as _BaseLayoutHierarchyType
21
import copy as _copy
2+
import warnings
3+
4+
from plotly.basedatatypes import BaseLayoutHierarchyType as _BaseLayoutHierarchyType
35

46

57
class Template(_BaseLayoutHierarchyType):
6-
78
# class properties
89
# --------------------
910
_parent_path_str = "layout"
@@ -324,7 +325,13 @@ def __init__(self, arg=None, data=None, layout=None, **kwargs):
324325
_v = arg.pop("data", None)
325326
_v = data if data is not None else _v
326327
if _v is not None:
327-
self["data"] = _v
328+
# Template.data contains a 'scattermapbox' key, which causes a
329+
# go.Scattermapbox trace object to be created during validation.
330+
# In order to prevent false deprecation warnings from surfacing,
331+
# we suppress deprecation warnings for this line only.
332+
with warnings.catch_warnings():
333+
warnings.filterwarnings("ignore", category=DeprecationWarning)
334+
self["data"] = _v
328335
_v = arg.pop("layout", None)
329336
_v = layout if layout is not None else _v
330337
if _v is not None:

tests/test_core/test_graph_objs/test_graph_objs.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from unittest import TestCase
2+
import warnings
23

4+
import pytest
35
import plotly.graph_objs as go
46

57

@@ -46,7 +48,6 @@
4648

4749
class TestBackwardsCompat(TestCase):
4850
def test_old_class_names(self):
49-
5051
# these were all defined at one point, we want to maintain backwards
5152
# compat, so we basically just create a checkpoint with this test.
5253

@@ -154,3 +155,29 @@ def test_pop_invalid_prop_key_error(self):
154155

155156
def test_pop_invalid_prop_with_default(self):
156157
self.assertEqual(self.layout.pop("bogus", 42), 42)
158+
159+
160+
class TestDeprecationWarnings(TestCase):
161+
def test_warn_on_deprecated_mapbox_traces(self):
162+
# This test will fail if any of the following traces
163+
# fails to emit a DeprecationWarning
164+
for trace_constructor in [
165+
go.Scattermapbox,
166+
go.Densitymapbox,
167+
go.Choroplethmapbox,
168+
]:
169+
with pytest.warns(DeprecationWarning):
170+
_ = go.Figure([trace_constructor()])
171+
172+
def test_no_warn_on_non_deprecated_traces(self):
173+
# This test will fail if any of the following traces emits a DeprecationWarning
174+
for trace_constructor in [
175+
go.Scatter,
176+
go.Bar,
177+
go.Scattermap,
178+
go.Densitymap,
179+
go.Choroplethmap,
180+
]:
181+
with warnings.catch_warnings():
182+
warnings.simplefilter("error")
183+
_ = go.Figure([trace_constructor()])

tests/test_optional/test_px/test_px.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from itertools import permutations
2+
import warnings
3+
14
import plotly.express as px
25
import plotly.io as pio
36
import narwhals.stable.v1 as nw
47
import numpy as np
58
import pytest
6-
from itertools import permutations
79

810

911
def test_scatter(backend):
@@ -394,3 +396,49 @@ def test_load_px_data(return_type):
394396
else:
395397
df = getattr(px.data, fname)(return_type=return_type)
396398
assert len(df) > 0
399+
400+
401+
def test_warn_on_deprecated_mapbox_px_constructors():
402+
# This test will fail if any of the following px constructors
403+
# fails to emit a DeprecationWarning
404+
for fig_constructor in [
405+
px.line_mapbox,
406+
px.scatter_mapbox,
407+
px.density_mapbox,
408+
px.choropleth_mapbox,
409+
]:
410+
# Look for warnings with the string "_mapbox" in them
411+
# to make sure the warning is coming from px rather than go
412+
with pytest.warns(DeprecationWarning, match="_mapbox"):
413+
if fig_constructor == px.choropleth_mapbox:
414+
fig_constructor(locations=["CA", "TX", "NY"])
415+
else:
416+
fig_constructor(lat=[10, 20, 30], lon=[10, 20, 30])
417+
418+
419+
def test_no_warn_on_non_deprecated_px_constructors():
420+
# This test will fail if any of the following px constructors
421+
# emits a DeprecationWarning
422+
for fig_constructor in [
423+
px.scatter,
424+
px.line,
425+
px.scatter_map,
426+
px.density_map,
427+
px.choropleth_map,
428+
]:
429+
with warnings.catch_warnings():
430+
warnings.simplefilter("error")
431+
if fig_constructor == px.choropleth_map:
432+
fig_constructor(locations=["CA", "TX", "NY"])
433+
elif fig_constructor in {px.scatter_map, px.density_map}:
434+
fig_constructor(lat=[10, 20, 30], lon=[10, 20, 30])
435+
else:
436+
fig_constructor(x=[1, 2, 3], y=[1, 2, 3])
437+
438+
439+
def test_no_warn_on_update_template():
440+
# This test will fail if update_layout(template=...) emits a DeprecationWarning
441+
fig = px.line(x=[1, 2, 3], y=[1, 2, 3])
442+
with warnings.catch_warnings():
443+
warnings.simplefilter("error")
444+
fig.update_layout(template="plotly_white")

0 commit comments

Comments
 (0)