Skip to content

Commit bbaea5b

Browse files
authored
Deprecate setting a parameter value before full instance initialization (#1025)
1 parent e96a798 commit bbaea5b

4 files changed

Lines changed: 31 additions & 3 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+
| `ParamPendingDeprecationWarning` since `2.3.0` | `param.parameterized` module / Setting a parameter value before full instance initialization |
78
| `ParamFutureWarning` since `2.2.0`, `ParamDeprecationWarning` since `2.0.0` | Parameter slots / `List._class`: use instead `item_type` |
89
| `ParamFutureWarning` since `2.2.0`, `ParamDeprecationWarning` since `2.0.0` | Parameter slots / `Number.set_hook`: no replacement |
910
| `ParamFutureWarning` since `2.2.0`, `ParamDeprecationWarning` since `2.0.0` | `param.__init__` module / `param.produce_value`: no replacement |

param/parameterized.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from ._utils import (
4646
DEFAULT_SIGNATURE,
4747
ParamFutureWarning as _ParamFutureWarning,
48+
ParamPendingDeprecationWarning as _ParamPendingDeprecationWarning,
4849
Skip,
4950
_deprecated,
5051
_deprecate_positional_args,
@@ -1880,6 +1881,15 @@ def __set__(self, obj, val):
18801881
else:
18811882
# When setting a Parameter before calling super.
18821883
if not isinstance(obj._param__private, _InstancePrivate):
1884+
warnings.warn(
1885+
f"Setting the Parameter {self.name!r} to {val!r} before "
1886+
f"the Parameterized class {type(obj).__name__!r} is fully "
1887+
"instantiated is deprecated and will raise an error in "
1888+
"a future version. Ensure the value is set after calling "
1889+
"`super().__init__(**params)` in the constructor.",
1890+
category=_ParamPendingDeprecationWarning,
1891+
stacklevel=4,
1892+
)
18831893
obj._param__private = _InstancePrivate(
18841894
explicit_no_refs=type(obj)._param__private.explicit_no_refs
18851895
)

tests/testdeprecations.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ def test_deprecate_all_equal(self):
9797
with pytest.raises(param._utils.ParamFutureWarning):
9898
param.parameterized.all_equal(1, 1)
9999

100+
def test_deprecate_setting_parameter_before_init(self):
101+
class P(param.Parameterized):
102+
x = param.Parameter()
103+
104+
def __init__(self, **params):
105+
self.x = 10
106+
super().__init__(**params)
107+
with pytest.raises(param._utils.ParamPendingDeprecationWarning):
108+
P()
109+
100110

101111
class TestDeprecateParameters:
102112

tests/testparameterizedobject.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import inspect
33
import re
44
import unittest
5+
import warnings
56

67
import param
78
import numbergen
@@ -412,7 +413,9 @@ def cb(self):
412413
nonlocal count
413414
count += 1
414415

415-
p = P()
416+
with warnings.catch_warnings():
417+
warnings.filterwarnings('ignore', message='Setting the Parameter', category=param._utils.ParamPendingDeprecationWarning)
418+
p = P()
416419

417420
assert p.x == 1
418421
assert count == 0
@@ -429,7 +432,9 @@ def __init__(self, depth=0):
429432
self.sub = P(depth+1)
430433
super().__init__()
431434

432-
p = P()
435+
with warnings.catch_warnings():
436+
warnings.filterwarnings('ignore', message='Setting the Parameter', category=param._utils.ParamPendingDeprecationWarning)
437+
p = P()
433438

434439
assert p.value == 'B'
435440
assert p.sub.value == 'B'
@@ -454,7 +459,9 @@ def __init__(self, batched=True, **params):
454459

455460
# When b is instantiated the `batched` Parameter of B is set before
456461
# Parameterized.__init__ is called.
457-
b = B()
462+
with warnings.catch_warnings():
463+
warnings.filterwarnings('ignore', message='Setting the Parameter', category=param._utils.ParamPendingDeprecationWarning)
464+
b = B()
458465
assert b.batched is True
459466

460467
def test_instantiation_param_objects_before_super_subclass(self):

0 commit comments

Comments
 (0)