Skip to content

Commit cce9781

Browse files
authored
Fix improper JSON encoding exception when pillow module not installed (#1993)
4.4.0 regression. When the pillow module is not installed, and an attempt is made to JSON serialize a figure that includes unsupported object types, an "AttributeError: 'NoneType' object has no attribute 'Image'" exception was raised. With this fix, the proper TypeError exception is raised once more.
1 parent 89f4c5b commit cce9781

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Diff for: packages/python/plotly/_plotly_utils/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ def encode_as_decimal(obj):
197197
@staticmethod
198198
def encode_as_pil(obj):
199199
"""Attempt to convert PIL.Image.Image to base64 data uri"""
200-
pil = get_module("PIL")
201-
if isinstance(obj, pil.Image.Image):
200+
image = get_module("PIL.Image")
201+
if image is not None and isinstance(obj, image.Image):
202202
return ImageUriValidator.pil_image_to_uri(obj)
203203
else:
204204
raise NotEncodable

Diff for: packages/python/plotly/plotly/tests/test_core/test_utils/test_utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def test_nan_to_null(self):
1515
expected_result = '[1, null, null, null, "platypus"]'
1616
self.assertEqual(result, expected_result)
1717

18+
def test_invalid_encode_exception(self):
19+
with self.assertRaises(TypeError):
20+
_json.dumps({"a": {1}}, cls=PlotlyJSONEncoder)
21+
1822

1923
class TestGetByPath(TestCase):
2024
def test_get_by_path(self):

0 commit comments

Comments
 (0)