From ca743027d5478517111130f0f31f5dcd50ca50cb Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Thu, 7 May 2020 16:04:24 -0400 Subject: [PATCH] Fix `TypeError: unhashable type: 'Template'` during `Figure` construction when `plotly.io.templates.default` is set to a `Template` object rather than a string. --- CHANGELOG.md | 1 + packages/python/plotly/plotly/basedatatypes.py | 10 ++++++++-- .../tests/test_core/test_graph_objs/test_template.py | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b423962209..ec0906d5d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fix `AttributeError: module 'plotly.graph_objs' has no attribute 'FigureWidget'` exception on `from plotly.graph_objs import *` when `ipywidgets` is not installed. Error also occurred when importing `plotly.figure_factor`. It is now possible to import `plotly.graph_objs.FigureWidget` when `ipywidgets` is not installed, and an informative `ImportError` exception will be raised in the `FigureWidget` constructor ([#2443](https://github.com/plotly/plotly.py/issues/2443), [#1111](https://github.com/plotly/plotly.py/issues/1111)). + - Fix `TypeError: unhashable type: 'Template'` during `Figure` construction when `plotly.io.templates.default` is set to a `Template` object rather than a string. ## [4.7.0] - 2020-05-06 diff --git a/packages/python/plotly/plotly/basedatatypes.py b/packages/python/plotly/plotly/basedatatypes.py index 87da535702a..65f138f96c2 100644 --- a/packages/python/plotly/plotly/basedatatypes.py +++ b/packages/python/plotly/plotly/basedatatypes.py @@ -1951,8 +1951,14 @@ def _initialize_layout_template(self): if self._allow_disable_validation: self._layout_obj._validate = False try: - template_dict = pio.templates[pio.templates.default] - self._layout_obj.template = template_dict + if isinstance(pio.templates.default, BasePlotlyType): + # Template object. Don't want to actually import `Template` + # here for performance so we check against `BasePlotlyType` + template_object = pio.templates.default + else: + # Name of registered template object + template_object = pio.templates[pio.templates.default] + self._layout_obj.template = template_object finally: self._layout_obj._validate = self._validate diff --git a/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py b/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py index d6ff6dc8995..a3061dfe5aa 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py +++ b/packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py @@ -175,6 +175,12 @@ def test_template_in(self): def test_template_iter(self): self.assertIn("test_template", set(pio.templates)) + def test_template_default_as_object(self): + template = go.layout.Template({"layout": {"font": {"family": "Rockwell"}}}) + pio.templates.default = template + fig = go.Figure() + self.assertEqual(fig.layout.template, template) + class TestToTemplated(TestCaseNoTemplate): def test_move_layout_nested_properties(self):