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

Fix: Display deprecated warnings for mapbox traces #4900

Merged
merged 8 commits into from
Nov 25, 2024
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
23 changes: 23 additions & 0 deletions packages/python/plotly/codegen/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
from codegen.utils import PlotlyNode, write_source_py


deprecated_mapbox_traces = [
"scattermapbox",
"choroplethmapbox",
"densitymapbox",
]


def get_typing_type(plotly_type, array_ok=False):
"""
Get Python type corresponding to a valType string from the plotly schema
Expand Down Expand Up @@ -95,6 +102,9 @@ def build_datatype_py(node):
)
buffer.write(f"import copy as _copy\n")

if node.name_property in deprecated_mapbox_traces:
buffer.write(f"from warnings import warn\n")

# Write class definition
# ----------------------
buffer.write(
Expand Down Expand Up @@ -400,6 +410,19 @@ def __init__(self"""
"""
)

if node.name_property in deprecated_mapbox_traces:
buffer.write(
f"""
warn(
"*{node.name_property}* is deprecated!"
+ " Use *{node.name_property.replace("mapbox", "map")}* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emilykl Good improvement. And I can approve this PR.
As follow ups to this PR:
Could you possibly

  1. double check that stacklevel=2 is the best option we could use?
  2. should we use similar stacklevel and category options for other warnings in plotly.py?
    Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe stacklevel=2 is the correct option in this case -- it causes the warning to appear to be emitted at the place in the user's code where they have called the deprecated function, for example:

Making a go.Choroplethmapbox figure...
/Users/ekl/code/plotly.py/my_script.py:32: DeprecationWarning:

*choroplethmapbox* is deprecated! Use *choroplethmap* instead. Learn more at: https://plotly.com/python/mapbox-to-maplibre/

(Also, for DeprecationWarning specifically, using any other stacklevel for these deprecations causes the warning to not appear at all.)

It's just coincidence that stacklevel=2 is the correct option in this case for both the go and px deprecations... the correct stacklevel will differ in every case depending on exactly where the warning is being emitted.

I don't think it's worth the effort to go through the codebase and clean up existing warnings, but I do think it makes sense to use stacklevel and category consistently going forward. It seems at least some of the warnings already in place in Plotly.py are using them correctly.

)
"""
)

# Return source string
# --------------------
return buffer.getvalue()
Expand Down
38 changes: 38 additions & 0 deletions packages/python/plotly/plotly/express/_chart_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from warnings import warn

from ._core import make_figure
from ._doc import make_docstring
import plotly.graph_objs as go
Expand Down Expand Up @@ -1415,9 +1417,18 @@ def scatter_mapbox(
height=None,
) -> go.Figure:
"""
*scatter_mapbox* is deprecated! Use *scatter_map* instead.
Learn more at: https://plotly.com/python/mapbox-to-maplibre/
In a Mapbox scatter plot, each row of `data_frame` is represented by a
symbol mark on a Mapbox map.
"""
warn(
"*scatter_mapbox* is deprecated!"
+ " Use *scatter_map* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)
return make_figure(args=locals(), constructor=go.Scattermapbox)


Expand Down Expand Up @@ -1453,9 +1464,18 @@ def choropleth_mapbox(
height=None,
) -> go.Figure:
"""
*choropleth_mapbox* is deprecated! Use *choropleth_map* instead.
Learn more at: https://plotly.com/python/mapbox-to-maplibre/
In a Mapbox choropleth map, each row of `data_frame` is represented by a
colored region on a Mapbox map.
"""
warn(
"*choropleth_mapbox* is deprecated!"
+ " Use *choropleth_map* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)
return make_figure(args=locals(), constructor=go.Choroplethmapbox)


Expand Down Expand Up @@ -1489,9 +1509,18 @@ def density_mapbox(
height=None,
) -> go.Figure:
"""
*density_mapbox* is deprecated! Use *density_map* instead.
Learn more at: https://plotly.com/python/mapbox-to-maplibre/
In a Mapbox density map, each row of `data_frame` contributes to the intensity of
the color of the region around the corresponding point on the map
"""
warn(
"*density_mapbox* is deprecated!"
+ " Use *density_map* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)
return make_figure(
args=locals(), constructor=go.Densitymapbox, trace_patch=dict(radius=radius)
)
Expand Down Expand Up @@ -1526,9 +1555,18 @@ def line_mapbox(
height=None,
) -> go.Figure:
"""
*line_mapbox* is deprecated! Use *line_map* instead.
Learn more at: https://plotly.com/python/mapbox-to-maplibre/
In a Mapbox line plot, each row of `data_frame` is represented as
a vertex of a polyline mark on a Mapbox map.
"""
warn(
"*line_mapbox* is deprecated!"
+ " Use *line_map* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)
return make_figure(args=locals(), constructor=go.Scattermapbox)


Expand Down
9 changes: 9 additions & 0 deletions packages/python/plotly/plotly/graph_objs/_choroplethmapbox.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from plotly.basedatatypes import BaseTraceType as _BaseTraceType
import copy as _copy
from warnings import warn


class Choroplethmapbox(_BaseTraceType):
Expand Down Expand Up @@ -2378,3 +2379,11 @@ def __init__(
# Reset skip_invalid
# ------------------
self._skip_invalid = False

warn(
"*choroplethmapbox* is deprecated!"
+ " Use *choroplethmap* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)
9 changes: 9 additions & 0 deletions packages/python/plotly/plotly/graph_objs/_densitymapbox.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from plotly.basedatatypes import BaseTraceType as _BaseTraceType
import copy as _copy
from warnings import warn


class Densitymapbox(_BaseTraceType):
Expand Down Expand Up @@ -2319,3 +2320,11 @@ def __init__(
# Reset skip_invalid
# ------------------
self._skip_invalid = False

warn(
"*densitymapbox* is deprecated!"
+ " Use *densitymap* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)
9 changes: 9 additions & 0 deletions packages/python/plotly/plotly/graph_objs/_scattermapbox.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from plotly.basedatatypes import BaseTraceType as _BaseTraceType
import copy as _copy
from warnings import warn


class Scattermapbox(_BaseTraceType):
Expand Down Expand Up @@ -2292,3 +2293,11 @@ def __init__(
# Reset skip_invalid
# ------------------
self._skip_invalid = False

warn(
"*scattermapbox* is deprecated!"
+ " Use *scattermap* instead."
+ " Learn more at: https://plotly.com/python/mapbox-to-maplibre/",
stacklevel=2,
category=DeprecationWarning,
)