diff --git a/README.rst b/README.rst index 0bdedfa4f..c9c74c321 100644 --- a/README.rst +++ b/README.rst @@ -104,20 +104,6 @@ UltraPlot is published on `PyPi `__ and pip install ultraplot conda install -c conda-forge ultraplot -pyCirclize-based plots require the optional ``circos`` extra: - -.. code-block:: bash - - pip install 'ultraplot[circos]' - -The ``docs`` extra also includes pyCirclize for building the documentation. - -To install the ``circos``, ``docs``, and ``stats`` dependency groups together: - -.. code-block:: bash - - pip install 'ultraplot[all]' - Likewise, an existing installation of UltraPlot can be upgraded to the latest version with: diff --git a/ultraplot/axes/plot_types/circlize.py b/ultraplot/axes/plot_types/circlize.py index 7fe11162f..e322564bc 100644 --- a/ultraplot/axes/plot_types/circlize.py +++ b/ultraplot/axes/plot_types/circlize.py @@ -21,6 +21,15 @@ #: saved, so `_import_pycirclize` puts these back. _PYCIRCLIZE_RC_LEAKS = ("savefig.bbox", "savefig.pad_inches", "svg.fonttype") +_PYCIRCLIZE_INSTALL_MESSAGE = ( + "This plot requires the optional pyCirclize dependency, which is not " + "available in this Python environment. Chord diagrams, radar charts, " + "phylogenies, and Circos plots need it.\n" + "Install it in the environment running your code:\n" + " pip install 'ultraplot[circos]'\n" + "Then restart Python or your notebook kernel and run the plot again." +) + def _import_pycirclize(): """ @@ -56,17 +65,9 @@ def _import_pycirclize_unguarded(): try: import pycirclize except ImportError as exc2: - raise ImportError( - "pycirclize is required for circos plots. Install it with " - "`pip install 'ultraplot[circos]'` or ensure " - "`pyCirclize/src` is on PYTHONPATH." - ) from exc2 + raise ImportError(_PYCIRCLIZE_INSTALL_MESSAGE) from exc2 else: - raise ImportError( - "pycirclize is required for circos plots. Install it with " - "`pip install 'ultraplot[circos]'` or ensure " - "`pyCirclize/src` is on PYTHONPATH." - ) from exc + raise ImportError(_PYCIRCLIZE_INSTALL_MESSAGE) from exc return pycirclize diff --git a/ultraplot/tests/test_circlize_integration.py b/ultraplot/tests/test_circlize_integration.py index 1995feba1..d5656e536 100644 --- a/ultraplot/tests/test_circlize_integration.py +++ b/ultraplot/tests/test_circlize_integration.py @@ -166,7 +166,23 @@ def test_circos_bed_plot_toggle(fake_pycirclize, tmp_path): uplt.close(fig) -def test_import_pycirclize_error_message(monkeypatch): +@pytest.mark.parametrize( + "method, args, kwargs", + [ + (None, (), {}), + ("circos", ({"A": 10},), {}), + ("chord_diagram", ({"A": {"A": 1}},), {}), + ("chord", ({"A": {"A": 1}},), {}), + ("radar_chart", ({"A": [1, 2]},), {"vmin": 0, "vmax": 3}), + ("radar", ({"A": [1, 2]},), {"vmin": 0, "vmax": 3}), + ("phylogeny", ("((A,B),C);",), {}), + ("circos_bed", ("missing.bed",), {}), + ("bed", ("missing.bed",), {}), + ], +) +def test_import_pycirclize_error_message(monkeypatch, method, args, kwargs): + """Every circular plot entry point explains how to install its dependency.""" + fig, ax = uplt.subplots(proj="polar") orig_import = builtins.__import__ def fake_import(name, *args, **kwargs): @@ -176,12 +192,19 @@ def fake_import(name, *args, **kwargs): monkeypatch.setattr(builtins, "__import__", fake_import) monkeypatch.setattr(Path, "is_dir", lambda self: False) - sys.modules.pop("pycirclize", None) - with pytest.warns(UserWarning, match=r"pip install 'ultraplot\[circos\]'"): - with pytest.raises( - ImportError, match="pycirclize is required for circos plots" - ): - circlize_mod._import_pycirclize() + monkeypatch.delitem(sys.modules, "pycirclize", raising=False) + try: + with pytest.warns( + UserWarning, match=r"pip install 'ultraplot\[circos\]'" + ) as caught: + with pytest.raises(ImportError, match="optional pyCirclize dependency"): + if method is None: + circlize_mod._import_pycirclize() + else: + getattr(ax, method)(*args, **kwargs) + assert "restart Python or your notebook kernel" in str(caught[0].message) + finally: + uplt.close(fig) def test_resolve_defaults_with_existing_objects(fake_pycirclize):