Skip to content

Lazy-import plotly in batch_plots so a missing dep can't cascade into unrelated test failures #1656

@AlexFiliakov

Description

@AlexFiliakov

Context

ergodic_insurance/visualization/batch_plots.py:20 does a hard module-top
import plotly.graph_objects as go, and visualization/__init__.py imports batch_plots
eagerly. So if plotly is absent, importing the visualization package raises
ModuleNotFoundError: No module named 'plotly' at import time, which cascades into ~26
unrelated reporting tests
(test_misc_gaps_coverage.py::TestTableGenerator::*,
TestScenarioComparator::*) — none of which use plotly.

This is not a correctness bug and does not affect CI: plotly>=5.18.0 is a declared
dependency (pyproject.toml [project].dependencies), so CI and the project .venv have it
and these tests pass. It surfaced only when running tests on an interpreter that lacked plotly.
This issue is an optional robustness / dev-ergonomics hardening so one missing
(optional-feeling) dependency can't take down the entire viz package import + dozens of
unrelated tests.

Scope

Only one function uses plotly: plot_parameter_sweep_3d (go.Figure, go.Scatter3d,
and the return annotation -> go.Figure). The other four plotters
(plot_scenario_comparison, plot_sensitivity_heatmap, plot_scenario_convergence,
plot_parallel_scenarios) are matplotlib-only.

Proposed change

  • Move import plotly.graph_objects as go from module scope into plot_parameter_sweep_3d.
  • Make the return annotation not require go at import time — from __future__ import annotations, a string annotation -> "go.Figure", or -> Any.
  • Optionally wrap the in-function import in try/except ImportError with a friendly
    "pip install plotly" hint.

Acceptance criteria

  • import ergodic_insurance.visualization (and ergodic_insurance.visualization.batch_plots) succeeds when plotly is not installed.
  • The matplotlib-only plotters and the reporting tests (TestTableGenerator / TestScenarioComparator) collect and pass without plotly installed.
  • plot_parameter_sweep_3d still works when plotly IS installed, and raises a clear, actionable error when it is not.
  • plotly stays a declared dependency (making it an optional extra is out of scope here).
  • lint / type-check clean.

Notes

Surfaced during PR #1655 review. Lower priority because CI and .venv are unaffected — this
only bites a partial/non-.venv local environment.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions