Skip to content

Commit

Permalink
Fix ValueError in get_graph_kwargs (bokeh#14022)
Browse files Browse the repository at this point in the history
* Fix ValueError in get_graph_kwargs

* Add Test_get_graph_kwargs::test_bad_input
  • Loading branch information
cdeil authored Aug 14, 2024
1 parent 3ed9201 commit f6ded42
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/bokeh/plotting/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,20 @@
# Dev API
#-----------------------------------------------------------------------------

def get_graph_kwargs(node_source: ColumnDataSource, edge_source: ColumnDataSource, **kwargs):
def get_graph_kwargs(node_source: ColumnDataSource, edge_source: ColumnDataSource, **kwargs) -> dict:

if not isinstance(node_source, ColumnarDataSource):
try:
# try converting the source to ColumnDataSource
node_source = ColumnDataSource(node_source)
except ValueError as err:
msg = f"Failed to auto-convert {type(node_source)} to ColumnDataSource.\n Original error: {err.message}"
msg = f"Failed to auto-convert {type(node_source)} to ColumnDataSource.\n Original error: {err}"
raise ValueError(msg).with_traceback(sys.exc_info()[2])

if not isinstance(edge_source, ColumnarDataSource):
try:
# try converting the source to ColumnDataSource
edge_source = ColumnDataSource(edge_source)
except ValueError as err:
msg = f"Failed to auto-convert {type(edge_source)} to ColumnDataSource.\n Original error: {err.message}"
msg = f"Failed to auto-convert {type(edge_source)} to ColumnDataSource.\n Original error: {err}"
raise ValueError(msg).with_traceback(sys.exc_info()[2])

marker = kwargs.pop('node_marker', None)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/bokeh/plotting/test__graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ def test_default_muted_glyph(self) -> None:
assert r.muted_glyph.line_alpha == 0.2
assert r.muted_glyph.line_color == "blue"

def test_bad_input(self) -> None:
msg = "Failed to auto-convert <class 'int'> to ColumnDataSource.\n Original error: expected a dict or pandas.DataFrame, got 42"
with pytest.raises(ValueError, match=msg):
bpg.get_graph_kwargs(42, {})

with pytest.raises(ValueError, match=msg):
bpg.get_graph_kwargs({}, 42)

#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------

0 comments on commit f6ded42

Please sign in to comment.