Skip to content

Commit 2c23521

Browse files
fix: type-check report code against optional deps in CI (#798)
1 parent 6b8d7a4 commit 2c23521

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎.github/workflows/main.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ jobs:
3030
cache: pip
3131
cache-dependency-path: pyproject.toml # it would be better on a lock file
3232
- name: Install dev dependencies
33+
# mypy needs `test` optional dependencies that ship types
3334
run: |
3435
python -m pip install --upgrade pip
35-
python -m pip install '.[dev]'
36+
python -m pip install '.[dev,test]'
3637
- name: Check linting
3738
run: make lint
3839
- name: Check formatting

‎shapash/report/blocks.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ def _render_custom(self, block_cfg: ReportBlockConfig) -> pn.viewable.Viewable:
188188
return result
189189
if isinstance(result, str):
190190
return pn.pane.Markdown(result)
191-
return pn.panel(result)
191+
viewable = pn.panel(result)
192+
if not isinstance(viewable, pn.viewable.Viewable):
193+
raise TypeError(f"{func_path} returned {type(result).__name__}, which is not embeddable in a report.")
194+
return viewable
192195
except Exception as exc:
193196
logger.error("Custom block '%s' raised: %s", func_path, exc)
194197
return render_block_error(func_path, exc)
@@ -381,6 +384,8 @@ def _truncate(value: Any, max_len: int) -> str:
381384
text = str(value)
382385
return text if len(text) <= max_len else text[: max_len - 3] + "..."
383386

387+
# A tuple is rendered side by side as a row by the @block decorator
388+
params_table: pn.pane.DataFrame | tuple[Any, ...]
384389
if len(params_items) > 15:
385390
split_idx = len(params_items) // 2
386391
left_df = pd.DataFrame(

‎tests/unit_tests/report/test_smart_report_panel.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,29 @@ def test_block_decorator_rejects_non_panel_return_type(self):
179179
runtime.block_non_panel_type_not_allowed()
180180

181181
self.assertIn("Unsupported block return type", str(context.exception))
182+
183+
184+
def _custom_returns_plain_object(runtime):
185+
return {"a": 1}
186+
187+
188+
def _custom_returns_template(runtime):
189+
return pn.template.BootstrapTemplate()
190+
191+
192+
class TestRenderCustomBlock(unittest.TestCase):
193+
def _render(self, func_name):
194+
return _DummyBlocks().render_block({"type": "custom", "function": f"{__name__}.{func_name}", "params": {}})
195+
196+
def test_custom_block_coerces_plain_object_with_pn_panel(self):
197+
result = self._render("_custom_returns_plain_object")
198+
199+
self.assertIsInstance(result, pn.viewable.Viewable)
200+
self.assertNotIsInstance(result, pn.pane.Alert)
201+
202+
def test_custom_block_returning_non_viewable_renders_error(self):
203+
result = self._render("_custom_returns_template")
204+
205+
self.assertIsInstance(result, pn.pane.Alert)
206+
self.assertIn("BootstrapTemplate", result.object)
207+
self.assertIn("not embeddable in a report", result.object)

0 commit comments

Comments
 (0)