Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress DeprecationWarning when updating template.data #5080

Merged
merged 4 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions plotly/graph_objs/layout/_template.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from plotly.basedatatypes import BaseLayoutHierarchyType as _BaseLayoutHierarchyType
import copy as _copy
import warnings

from plotly.basedatatypes import BaseLayoutHierarchyType as _BaseLayoutHierarchyType


class Template(_BaseLayoutHierarchyType):

# class properties
# --------------------
_parent_path_str = "layout"
Expand Down Expand Up @@ -324,7 +325,13 @@ def __init__(self, arg=None, data=None, layout=None, **kwargs):
_v = arg.pop("data", None)
_v = data if data is not None else _v
if _v is not None:
self["data"] = _v
# Template.data contains a 'scattermapbox' key, which causes a
# go.Scattermapbox trace object to be created during validation.
# In order to prevent false deprecation warnings from surfacing,
# we suppress deprecation warnings for this line only.
with warnings.catch_warnings():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you

warnings.filterwarnings("ignore", category=DeprecationWarning)
self["data"] = _v
_v = arg.pop("layout", None)
_v = layout if layout is not None else _v
if _v is not None:
Expand Down
29 changes: 28 additions & 1 deletion tests/test_core/test_graph_objs/test_graph_objs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import TestCase
import warnings

import pytest
import plotly.graph_objs as go


Expand Down Expand Up @@ -46,7 +48,6 @@

class TestBackwardsCompat(TestCase):
def test_old_class_names(self):

# these were all defined at one point, we want to maintain backwards
# compat, so we basically just create a checkpoint with this test.

Expand Down Expand Up @@ -154,3 +155,29 @@ def test_pop_invalid_prop_key_error(self):

def test_pop_invalid_prop_with_default(self):
self.assertEqual(self.layout.pop("bogus", 42), 42)


class TestDeprecationWarnings(TestCase):
def test_warn_on_deprecated_mapbox_traces(self):
# This test will fail if any of the following traces
# fails to emit a DeprecationWarning
for trace_constructor in [
go.Scattermapbox,
go.Densitymapbox,
go.Choroplethmapbox,
]:
with pytest.warns(DeprecationWarning):
_ = go.Figure([trace_constructor()])

def test_no_warn_on_non_deprecated_traces(self):
# This test will fail if any of the following traces emits a DeprecationWarning
for trace_constructor in [
go.Scatter,
go.Bar,
go.Scattermap,
go.Densitymap,
go.Choroplethmap,
]:
with warnings.catch_warnings():
warnings.simplefilter("error")
_ = go.Figure([trace_constructor()])
50 changes: 49 additions & 1 deletion tests/test_optional/test_px/test_px.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from itertools import permutations
import warnings

import plotly.express as px
import plotly.io as pio
import narwhals.stable.v1 as nw
import numpy as np
import pytest
from itertools import permutations


def test_scatter(backend):
Expand Down Expand Up @@ -394,3 +396,49 @@ def test_load_px_data(return_type):
else:
df = getattr(px.data, fname)(return_type=return_type)
assert len(df) > 0


def test_warn_on_deprecated_mapbox_px_constructors():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

# This test will fail if any of the following px constructors
# fails to emit a DeprecationWarning
for fig_constructor in [
px.line_mapbox,
px.scatter_mapbox,
px.density_mapbox,
px.choropleth_mapbox,
]:
# Look for warnings with the string "_mapbox" in them
# to make sure the warning is coming from px rather than go
with pytest.warns(DeprecationWarning, match="_mapbox"):
if fig_constructor == px.choropleth_mapbox:
fig_constructor(locations=["CA", "TX", "NY"])
else:
fig_constructor(lat=[10, 20, 30], lon=[10, 20, 30])


def test_no_warn_on_non_deprecated_px_constructors():
# This test will fail if any of the following px constructors
# emits a DeprecationWarning
for fig_constructor in [
px.scatter,
px.line,
px.scatter_map,
px.density_map,
px.choropleth_map,
]:
with warnings.catch_warnings():
warnings.simplefilter("error")
if fig_constructor == px.choropleth_map:
fig_constructor(locations=["CA", "TX", "NY"])
elif fig_constructor in {px.scatter_map, px.density_map}:
fig_constructor(lat=[10, 20, 30], lon=[10, 20, 30])
else:
fig_constructor(x=[1, 2, 3], y=[1, 2, 3])


def test_no_warn_on_update_template():
# This test will fail if update_layout(template=...) emits a DeprecationWarning
fig = px.line(x=[1, 2, 3], y=[1, 2, 3])
with warnings.catch_warnings():
warnings.simplefilter("error")
fig.update_layout(template="plotly_white")