Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the Parameter attribute pickle_default_value #1019

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/user_guide/Serialization_and_Persistence.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"\n",
"1. **Callable parameter values**: If you provide any `param.Callable`, `param.Hooklist`, or other parameters that can accept callable objects to your users, you will need to warn them that none of those can be set to unnamed (lambda) functions or to one-off functions defined in the main namespace if they want to use pickling. Of course, you can accept such values during initial development when you may not care about pickling, but once things are working, move the one-off function to a proper importable module and then it will be safe to use as a picklable value. One way to make this work smoothly is to create `param.ParameterizedFunction` objects or other \"function object\" classes (classes whose instances are callable like functions but which may have state and are fully picklable); see e.g. the `numbergen` module for examples.\n",
"\n",
"2. **Skipping Parameters that should not be pickled**: In some cases, you may not _want_ the value of a given Parameter to be pickled and restored even while other state is being serialized. For instance, a Parameter whose value is set to a particular file path might cause errors if that path is restored when the pickle is loaded on a different system or once the file no longer exists. To cover such rare but potentially important cases, the Parameter can be defined with `pickle_default_value=False` (normally `True`), so that the instantaneous value is usable but won't be saved and restored with pickle.\n",
"2. **Skipping Parameters that should not be pickled [DEPRECATED in version 2.3.0]**: In some cases, you may not _want_ the value of a given Parameter to be pickled and restored even while other state is being serialized. For instance, a Parameter whose value is set to a particular file path might cause errors if that path is restored when the pickle is loaded on a different system or once the file no longer exists. To cover such rare but potentially important cases, the Parameter can be defined with `pickle_default_value=False` (normally `True`), so that the instantaneous value is usable but won't be saved and restored with pickle.\n",
"\n",
"3. **Customizing settting and getting state**: You may find that your Parameter or Parameterized objects have other state that you need to handle specially, whether that's to save and restore data that isn't otherwise picklable, or to ignore state that should _not_ be pickled. For instance, if your object's dictionary contains some object that doesn't support pickling, then you can add code to omit that or to serialize it in some special way that allows it to be restored, e.g. by extracting a state dictionary fom it and then restoring it from the dictionary later. See the [pickle docs](https://docs.python.org/3/library/pickle.html#pickle-state) for the `__getstate__` and `__setstate__` methods that you can implement on your Parameter or Parameterized objects to override this behavior. Be sure to call `super(YourClass,self).__setstate__(state)` or the getstate equivalent so that you also store parameters and dictionary values as usual, if desired.\n",
"\n",
Expand Down
7 changes: 7 additions & 0 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from . import serializer
from ._utils import (
DEFAULT_SIGNATURE,
ParamDeprecationWarning as _ParamDeprecationWarning,
ParamFutureWarning as _ParamFutureWarning,
Skip,
_deprecated,
Expand Down Expand Up @@ -1305,6 +1306,12 @@ class hierarchy (see ParameterizedMetaclass).
self.readonly = readonly
self._label = label
self._set_instantiate(instantiate)
if pickle_default_value is False:
warnings.warn(
'pickle_default_value has been deprecated.',
category=_ParamDeprecationWarning,
stacklevel=3,
)
self.pickle_default_value = pickle_default_value
self._set_allow_None(allow_None)
self.watchers = {}
Expand Down
6 changes: 3 additions & 3 deletions param/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,11 +2610,11 @@ class resolve_path(ParameterizedFunction):
than just os.getcwd() can be used, and the file must exist.
"""

search_paths = List(default=[os.getcwd()], pickle_default_value=False, doc="""
search_paths = List(default=[os.getcwd()], pickle_default_value=None, doc="""
Prepended to a non-relative path, in order, until a file is
found.""")

path_to_file = Boolean(default=True, pickle_default_value=False,
path_to_file = Boolean(default=True, pickle_default_value=None,
allow_None=True, doc="""
String specifying whether the path refers to a 'File' or a
'Folder'. If None, the path may point to *either* a 'File' *or*
Expand Down Expand Up @@ -2665,7 +2665,7 @@ class normalize_path(ParameterizedFunction):
prefix rather than os.getcwd).
"""

prefix = String(default=os.getcwd(),pickle_default_value=False,doc="""
prefix = String(default=os.getcwd(),pickle_default_value=None,doc="""
Prepended to the specified path, if that path is not
absolute.""")

Expand Down
5 changes: 5 additions & 0 deletions tests/testdeprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class P(param.Parameterized):
with pytest.raises(param._utils.ParamFutureWarning):
p.n = 1

def test_deprecate_Parameter_pickle_default_value(self):
with pytest.raises(param._utils.ParamDeprecationWarning):
param.Parameter(pickle_default_value=False)



class TestDeprecateInitModule:

Expand Down