diff --git a/packages/python/plotly/_plotly_utils/utils.py b/packages/python/plotly/_plotly_utils/utils.py index a2275374ea0..d0d43134a48 100644 --- a/packages/python/plotly/_plotly_utils/utils.py +++ b/packages/python/plotly/_plotly_utils/utils.py @@ -197,8 +197,8 @@ def encode_as_decimal(obj): @staticmethod def encode_as_pil(obj): """Attempt to convert PIL.Image.Image to base64 data uri""" - pil = get_module("PIL") - if isinstance(obj, pil.Image.Image): + image = get_module("PIL.Image") + if image is not None and isinstance(obj, image.Image): return ImageUriValidator.pil_image_to_uri(obj) else: raise NotEncodable diff --git a/packages/python/plotly/plotly/tests/test_core/test_utils/test_utils.py b/packages/python/plotly/plotly/tests/test_core/test_utils/test_utils.py index 7a8b5ca094e..27b77bd1d4c 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_utils/test_utils.py +++ b/packages/python/plotly/plotly/tests/test_core/test_utils/test_utils.py @@ -15,6 +15,10 @@ def test_nan_to_null(self): expected_result = '[1, null, null, null, "platypus"]' self.assertEqual(result, expected_result) + def test_invalid_encode_exception(self): + with self.assertRaises(TypeError): + _json.dumps({"a": {1}}, cls=PlotlyJSONEncoder) + class TestGetByPath(TestCase): def test_get_by_path(self):