Skip to content

Commit ca74302

Browse files
committed
Fix TypeError: unhashable type: 'Template' during Figure construction
when `plotly.io.templates.default` is set to a `Template` object rather than a string.
1 parent ecb635a commit ca74302

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Fixed
88

99
- 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)).
10+
- Fix `TypeError: unhashable type: 'Template'` during `Figure` construction when `plotly.io.templates.default` is set to a `Template` object rather than a string.
1011

1112

1213
## [4.7.0] - 2020-05-06

Diff for: packages/python/plotly/plotly/basedatatypes.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1951,8 +1951,14 @@ def _initialize_layout_template(self):
19511951
if self._allow_disable_validation:
19521952
self._layout_obj._validate = False
19531953
try:
1954-
template_dict = pio.templates[pio.templates.default]
1955-
self._layout_obj.template = template_dict
1954+
if isinstance(pio.templates.default, BasePlotlyType):
1955+
# Template object. Don't want to actually import `Template`
1956+
# here for performance so we check against `BasePlotlyType`
1957+
template_object = pio.templates.default
1958+
else:
1959+
# Name of registered template object
1960+
template_object = pio.templates[pio.templates.default]
1961+
self._layout_obj.template = template_object
19561962
finally:
19571963
self._layout_obj._validate = self._validate
19581964

Diff for: packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_template.py

+6
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ def test_template_in(self):
175175
def test_template_iter(self):
176176
self.assertIn("test_template", set(pio.templates))
177177

178+
def test_template_default_as_object(self):
179+
template = go.layout.Template({"layout": {"font": {"family": "Rockwell"}}})
180+
pio.templates.default = template
181+
fig = go.Figure()
182+
self.assertEqual(fig.layout.template, template)
183+
178184

179185
class TestToTemplated(TestCaseNoTemplate):
180186
def test_move_layout_nested_properties(self):

0 commit comments

Comments
 (0)