From 7250704776c730b66e283e7dbe394d0254327b18 Mon Sep 17 00:00:00 2001 From: Emily KL <4672118+emilykl@users.noreply.github.com> Date: Tue, 20 May 2025 13:44:13 -0400 Subject: [PATCH 1/2] fix indentation --- plotly/basedatatypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/basedatatypes.py b/plotly/basedatatypes.py index 12b14c1ca85..443c34e826c 100644 --- a/plotly/basedatatypes.py +++ b/plotly/basedatatypes.py @@ -3908,7 +3908,7 @@ def write_image(self, *args, **kwargs): warnings.warn( ENGINE_PARAM_DEPRECATION_MSG, DeprecationWarning, stacklevel=2 ) - return pio.write_image(self, *args, **kwargs) + return pio.write_image(self, *args, **kwargs) # Static helpers # -------------- From 6c257931c8a88a534a75a68b0ad571115244fb28 Mon Sep 17 00:00:00 2001 From: Emily KL <4672118+emilykl@users.noreply.github.com> Date: Tue, 20 May 2025 14:15:55 -0400 Subject: [PATCH 2/2] add test to prevent regression --- .../test_kaleido/test_kaleido.py | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/tests/test_optional/test_kaleido/test_kaleido.py b/tests/test_optional/test_kaleido/test_kaleido.py index 9a9d414dc92..84b07772307 100644 --- a/tests/test_optional/test_kaleido/test_kaleido.py +++ b/tests/test_optional/test_kaleido/test_kaleido.py @@ -1,15 +1,18 @@ +import base64 +from contextlib import redirect_stdout from io import BytesIO, StringIO from pathlib import Path import tempfile -from contextlib import redirect_stdout -import base64 +from unittest.mock import patch from pdfrw import PdfReader from PIL import Image +import plotly.graph_objects as go import plotly.io as pio from plotly.io.kaleido import kaleido_available, kaleido_major import pytest + fig = {"data": [], "layout": {"title": {"text": "figure title"}}} @@ -160,3 +163,43 @@ def test_defaults(): finally: pio.defaults.default_format = "png" assert pio.defaults.default_format == "png" + + +def test_fig_write_image(): + """Test that fig.write_image() calls the correct underlying Kaleido function.""" + + test_fig = go.Figure(fig) + test_image_bytes = b"mock image data" + + if kaleido_major() > 0: + patch_funcname = "plotly.io._kaleido.kaleido.calc_fig_sync" + else: + patch_funcname = "plotly.io._kaleido.scope.transform" + + with patch(patch_funcname, return_value=test_image_bytes) as mock_calc_fig: + test_fig.write_image("test_path.png") + + # Verify patched function was called once with fig dict as first argument + mock_calc_fig.assert_called_once() + args, _ = mock_calc_fig.call_args + assert args[0] == test_fig.to_dict() + + +def test_fig_to_image(): + """Test that fig.to_image() calls the correct underlying Kaleido function.""" + + test_fig = go.Figure(fig) + test_image_bytes = b"mock image data" + + if kaleido_major() > 0: + patch_funcname = "plotly.io._kaleido.kaleido.calc_fig_sync" + else: + patch_funcname = "plotly.io._kaleido.scope.transform" + + with patch(patch_funcname, return_value=test_image_bytes) as mock_calc_fig: + test_fig.to_image() + + # Verify patched function was called once with fig dict as first argument + mock_calc_fig.assert_called_once() + args, _ = mock_calc_fig.call_args + assert args[0] == test_fig.to_dict()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: