diff --git a/holoviews/core/dimension.py b/holoviews/core/dimension.py index 56b348a637..fa05ac8e57 100644 --- a/holoviews/core/dimension.py +++ b/holoviews/core/dimension.py @@ -716,37 +716,27 @@ def __setstate__(self, d): "Restores options applied to this object." d = param_aliases(d) - # Backwards compatibility for objects before id was made a property - opts_id = d['_id'] if '_id' in d else d.pop('id', None) - try: - load_options = Store.load_counter_offset is not None - if load_options: - matches = [k for k in d if k.startswith('_custom_option')] - for match in matches: - custom_id = int(match.split('_')[-1])+Store.load_counter_offset - if not isinstance(d[match], dict): - # Backward compatibility before multiple backends - backend_info = {'matplotlib':d[match]} - else: - backend_info = d[match] - for backend, info in backend_info.items(): - if backend not in Store._custom_options: - Store._custom_options[backend] = {} - Store._custom_options[backend][custom_id] = info - if backend_info: - if custom_id not in Store._weakrefs: - Store._weakrefs[custom_id] = [] - ref = weakref.ref(self, partial(cleanup_custom_options, custom_id)) - Store._weakrefs[opts_id].append(ref) - d.pop(match) - - if opts_id is not None: - opts_id += Store.load_counter_offset - except Exception: - self.param.warning("Could not unpickle custom style information.") - d['_id'] = opts_id + load_options = Store.load_counter_offset is not None + if load_options: + matches = [k for k in d if k.startswith('_custom_option')] + for match in matches: + custom_id = int(match.split('_')[-1])+Store.load_counter_offset + for backend, info in d[match].items(): + if backend not in Store._custom_options: + Store._custom_options[backend] = {} + Store._custom_options[backend][custom_id] = info + if d[match]: + if custom_id not in Store._weakrefs: + Store._weakrefs[custom_id] = [] + ref = weakref.ref(self, partial(cleanup_custom_options, custom_id)) + Store._weakrefs[d["_id"]].append(ref) + d.pop(match) + + if d["_id"] is not None: + d["_id"] += Store.load_counter_offset + self.__dict__.update(d) - super().__setstate__({}) + super().__setstate__(d) class Dimensioned(LabelledData): diff --git a/holoviews/tests/core/test_dimensioned.py b/holoviews/tests/core/test_dimensioned.py index f65060deac..1dc3dc7a46 100644 --- a/holoviews/tests/core/test_dimensioned.py +++ b/holoviews/tests/core/test_dimensioned.py @@ -1,8 +1,10 @@ import gc +import pickle from holoviews.core.element import Element from holoviews.core.options import Keywords, Options, OptionTree, Store from holoviews.core.spaces import HoloMap +from holoviews.element.chart import Curve from ..utils import LoggingComparisonTestCase @@ -247,3 +249,11 @@ def test_opts_clear_cleans_unused_tree(self): ExampleElement([]).opts(style_opt1='A').opts.clear() custom_options = Store._custom_options['backend_1'] self.assertEqual(len(custom_options), 0) + +class TestGetSetState: + + def test_pickle_roundtrip(self): + curve = Curve([0, 1, 2], kdims=["XAXIS"]) + roundtrip_curve = pickle.loads(pickle.dumps(curve)) + assert curve.kdims == roundtrip_curve.kdims + assert curve.vdims == roundtrip_curve.vdims