Skip to content

Commit 6a22b1c

Browse files
authored
Deprecate the Parameter attribute pickle_default_value (#1019)
1 parent e6d7ae6 commit 6a22b1c

5 files changed

Lines changed: 19 additions & 4 deletions

File tree

doc/reference/deprecations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ List of currently deprecated APIs:
44

55
| Warning | Description |
66
|-|-|
7+
| `ParamDeprecationWarning` since `2.3.0` | Parameter slots / `Parameter.pickle_default_value`: no replacement |
78
| `ParamFutureWarning` since `2.3.0` | Parameterized `.param` namespace / `.param.watch_values`: the keyword `what` is deprecated |
89
| `ParamPendingDeprecationWarning` since `2.3.0` | `param.parameterized` module / Setting a parameter value before full instance initialization |
910
| `ParamFutureWarning` since `2.2.0`, `ParamDeprecationWarning` since `2.0.0` | Parameter slots / `List._class`: use instead `item_type` |

doc/user_guide/Serialization_and_Persistence.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
"\n",
144144
"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",
145145
"\n",
146-
"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",
146+
"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",
147147
"\n",
148148
"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",
149149
"\n",

param/parameterized.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from . import serializer
4646
from ._utils import (
4747
DEFAULT_SIGNATURE,
48+
ParamDeprecationWarning as _ParamDeprecationWarning,
4849
ParamFutureWarning as _ParamFutureWarning,
4950
ParamPendingDeprecationWarning as _ParamPendingDeprecationWarning,
5051
Skip,
@@ -1489,6 +1490,8 @@ def __init__( # pylint: disable-msg=R0913
14891490
pickle_default_value : bool, optional
14901491
Whether the default value should be pickled. Set to `False` in rare
14911492
cases, such as system-specific file paths.
1493+
1494+
.. deprecated:: 2.3.0
14921495
allow_None : bool, optional
14931496
If `True`, allows `None` as a valid parameter value. If the default
14941497
value is `None`, this is automatically set to `True`. Default is
@@ -1542,6 +1545,12 @@ def __init__( # pylint: disable-msg=R0913
15421545
self.readonly = readonly
15431546
self._label = label
15441547
self._set_instantiate(instantiate)
1548+
if pickle_default_value is False:
1549+
warnings.warn(
1550+
'pickle_default_value has been deprecated.',
1551+
category=_ParamDeprecationWarning,
1552+
stacklevel=3,
1553+
)
15451554
self.pickle_default_value = pickle_default_value
15461555
self._set_allow_None(allow_None)
15471556
self.watchers = {}

param/parameters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,11 +2618,11 @@ class resolve_path(ParameterizedFunction):
26182618
than just os.getcwd() can be used, and the file must exist.
26192619
"""
26202620

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

2625-
path_to_file = Boolean(default=True, pickle_default_value=False,
2625+
path_to_file = Boolean(default=True, pickle_default_value=None,
26262626
allow_None=True, doc="""
26272627
String specifying whether the path refers to a 'File' or a
26282628
'Folder'. If None, the path may point to *either* a 'File' *or*
@@ -2673,7 +2673,7 @@ class normalize_path(ParameterizedFunction):
26732673
prefix rather than os.getcwd).
26742674
"""
26752675

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

tests/testdeprecations.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class P(param.Parameterized):
3232
with pytest.raises(param._utils.ParamFutureWarning):
3333
p.n = 1
3434

35+
def test_deprecate_Parameter_pickle_default_value(self):
36+
with pytest.raises(param._utils.ParamDeprecationWarning):
37+
param.Parameter(pickle_default_value=False)
38+
39+
3540

3641
class TestDeprecateInitModule:
3742

0 commit comments

Comments
 (0)