Skip to content

Commit

Permalink
Attempt at backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpap committed Sep 20, 2023
1 parent 0c19c47 commit 406c044
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
58 changes: 58 additions & 0 deletions src/bokeh/models/annotations/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# Imports
#-----------------------------------------------------------------------------

# Standard library imports
from typing import Any

# Bokeh imports
from ...core.enums import CoordinateUnits, Dimension
from ...core.properties import (
Expand All @@ -35,19 +38,26 @@
Override,
Seq,
)
from ...core.property.singletons import Undefined
from ...core.property_aliases import BorderRadius
from ...core.property_mixins import ScalarFillProps, ScalarHatchProps, ScalarLineProps
from ...util.deprecation import deprecated
from .. import glyphs
from ..renderers import GlyphRenderer, Renderer
from ..sources import ColumnDataSource
from .annotation import Annotation

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

__all__ = (
"LegacyBand",
"BoxAnnotation",
"PolyAnnotation",
"Slope",
"Span",
"LegacyWhisker",
)

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -337,6 +347,54 @@ def __init__(self, *args, **kwargs) -> None:
hover_line_color = Override(default=None)
hover_line_alpha = Override(default=0.3)

#-----------------------------------------------------------------------------
# Legacy API
#-----------------------------------------------------------------------------

def LegacyBand(**kwargs: Any) -> GlyphRenderer:
""" Render a filled area band along a dimension.
"""
deprecated((3, 3, 0), "bokeh.annotations.Band", "bokeh.glyphs.Band or figure.band()")

defaults = dict(level="annotation")
glyph_renderer_kwargs = {}

for name in Renderer.properties():
default = defaults.get(name, Undefined)
value = kwargs.pop(name, default)
glyph_renderer_kwargs[name] = value

data_source = kwargs.pop("source", Undefined)
if data_source is Undefined:
data_source = ColumnDataSource()

Check warning on line 370 in src/bokeh/models/annotations/geometry.py

View check run for this annotation

Codecov / codecov/patch

src/bokeh/models/annotations/geometry.py#L370

Added line #L370 was not covered by tests

glyph = glyphs.Band(**kwargs)

return GlyphRenderer(data_source=data_source, glyph=glyph, **glyph_renderer_kwargs)

def LegacyWhisker(**kwargs: Any) -> GlyphRenderer:
""" Render whiskers along a dimension.
"""
deprecated((3, 3, 0), "bokeh.annotations.Whisker", "bokeh.glyphs.Whisker or figure.whisker()")

defaults = dict(level="annotation")
glyph_renderer_kwargs = {}

for name in Renderer.properties():
default = defaults.get(name, Undefined)
value = kwargs.pop(name, default)
glyph_renderer_kwargs[name] = value

data_source = kwargs.pop("source", Undefined)
if data_source is Undefined:
data_source = ColumnDataSource()

Check warning on line 392 in src/bokeh/models/annotations/geometry.py

View check run for this annotation

Codecov / codecov/patch

src/bokeh/models/annotations/geometry.py#L392

Added line #L392 was not covered by tests

glyph = glyphs.Whisker(**kwargs)

return GlyphRenderer(data_source=data_source, glyph=glyph, **glyph_renderer_kwargs)

#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
Expand Down
14 changes: 12 additions & 2 deletions src/bokeh/models/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@
Range,
Range1d,
)
from .renderers import GlyphRenderer, Renderer, TileRenderer
from .renderers import (
DataRenderer,
GlyphRenderer,
Renderer,
TileRenderer,
)
from .scales import (
CategoricalScale,
LinearScale,
Expand Down Expand Up @@ -299,7 +304,12 @@ def add_layout(self, obj: Renderer, place: PlaceType = "center") -> None:
f"Invalid place '{place}' specified. Valid place values are: {nice_join(Place)}",
)

getattr(self, place).append(obj)
if isinstance(obj, DataRenderer):
if place != "center":
raise ValueError(f"{obj} can only be added to 'center' region")
self.renderers.append(obj)
else:
getattr(self, place).append(obj)

def add_tools(self, *tools: Tool | str) -> None:
''' Adds tools to the plot.
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/bokeh/models/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@
GlyphRenderer,
Label,
LabelSet,
LegacyBand,
LegacyWhisker,
Legend,
LegendItem,
LinearColorMapper,
PolyAnnotation,
Slope,
Span,
Title,
glyphs,
)
from bokeh.util.serialization import convert_datetime_type
from bokeh.util.warnings import BokehDeprecationWarning

from _util_models import (
ABOVE_FILL,
Expand Down Expand Up @@ -519,6 +523,26 @@ def test_legend_item_with_field_label_raises_error_if_field_not_in_cds() -> None
process_validation_issues(issues)
assert mock_logger.error.call_count == 1

def test_legacy_Band() -> None:
data_source = ColumnDataSource()
with pytest.warns(BokehDeprecationWarning):
band = LegacyBand(name="band_annotation", source=data_source)
assert isinstance(band, GlyphRenderer)
assert isinstance(band.glyph, glyphs.Band)
assert band.data_source == data_source
assert band.name == "band_annotation"
assert band.level == "annotation"

def test_legacy_Whisker() -> None:
data_source = ColumnDataSource()
with pytest.warns(BokehDeprecationWarning):
whisker = LegacyWhisker(name="whisker_annotation", source=data_source)
assert isinstance(whisker, GlyphRenderer)
assert isinstance(whisker.glyph, glyphs.Whisker)
assert whisker.data_source == data_source
assert whisker.name == "whisker_annotation"
assert whisker.level == "annotation"

#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/bokeh/models/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ def test_plot_add_layout_adds_axis_to_renderers_and_side_renderers() -> None:
assert axis in plot.left


def test_plot_add_layout_adds_glyph_renderer_to_plot_renderers() -> None:
plot = figure()
glyph_renderer = GlyphRenderer()
plot.add_layout(glyph_renderer)
assert glyph_renderer in plot.renderers


def test_plot_add_layout_raises_when_adding_glyph_renderer_to_side_panels() -> None:
plot = figure()
glyph_renderer = GlyphRenderer()
with pytest.raises(ValueError):
plot.add_layout(glyph_renderer, "left")


def test_sizing_mode_property_is_fixed_by_default() -> None:
plot = figure()
assert plot.sizing_mode is None
Expand Down

0 comments on commit 406c044

Please sign in to comment.