File: testdeprecations.py

package info (click to toggle)
python-param 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,048 kB
  • sloc: python: 17,980; makefile: 3
file content (365 lines) | stat: -rw-r--r-- 12,949 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
Test deprecation warnings.
"""
import re
import warnings

import param
import pytest


@pytest.fixture(autouse=True)
def specific_filter():
    """
    Used to make sure warnings are set up with the right stacklevel.
    """
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        warnings.filterwarnings('error', module=__name__)
        yield


class TestDeprecateParameter:

    def test_deprecate_posargs_Parameter(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.Parameter(1, 'doc')

    def test_deprecate_List_class_(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.List(class_=int)

    def test_deprecate_Number_set_hook(self):
        class P(param.Parameterized):
            n = param.Number(set_hook=lambda obj, val: val)

        p = P()
        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.n = 1


class TestDeprecateInitModule:

    def test_deprecate_as_unicode(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.as_unicode(1)

    def test_deprecate_is_ordered_dict(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.is_ordered_dict(dict())

    def test_deprecate_produce_value(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.produce_value(1)

    def test_deprecate_hasbable(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.hashable('s')

    def test_deprecate_named_objs(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.named_objs(dict())

    def test_deprecate_normalize_path(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.normalize_path()

    def test_deprecate_abbreviate_path(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.abbreviate_paths()


class TestDeprecateParameterizedModule:

    def test_deprecate_overridable_property(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            class Foo:
                def _x(self): pass
                x = param.parameterized.overridable_property(_x)

    def test_deprecate_batch_watch(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            with param.parameterized.batch_watch(p):
                pass

    def test_deprecate_add_metaclass(self):
        class MC(type): pass

        with pytest.raises(param._utils.ParamDeprecationWarning):
            @param.parameterized.add_metaclass(MC)
            class Base: pass

    def test_deprecate_recursive_repr(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.parameterized.recursive_repr(lambda: '')

    def test_deprecate_all_equal(self):
        with pytest.raises(param._utils.ParamDeprecationWarning):
            param.parameterized.all_equal(1, 1)

    def test_deprecate_param_watchers(self):
        with pytest.raises(param._utils.ParamFutureWarning):
            param.parameterized.Parameterized()._param_watchers

    def test_deprecate_param_watchers_setter(self):
        with pytest.raises(param._utils.ParamFutureWarning):
            param.parameterized.Parameterized()._param_watchers = {}

    def test_param_error_unsafe_ops_before_initialized(self):
        class P(param.Parameterized):

            x = param.Parameter()

            def __init__(self, **params):
                with pytest.raises(
                    param._utils.ParamFutureWarning,
                    match=re.escape(
                        'Looking up instance Parameter objects (`.param.objects()`) until '
                        'the Parameterized instance has been fully initialized is deprecated and will raise an error in a future version. '
                        'Ensure you have called `super().__init__(**params)` in your Parameterized '
                        'constructor before trying to access instance Parameter objects, or '
                        'looking up the class Parameter objects with `.param.objects(instance=False)` '
                        'may be enough for your use case.',
                    )
                ):
                    self.param.objects()

                with pytest.raises(
                    param._utils.ParamFutureWarning,
                    match=re.escape(
                        'Triggering watchers on a partially initialized Parameterized instance '
                        'is deprecated and will raise an error in a future version. '
                        'Ensure you have called super().__init__(**params) in '
                        'the Parameterized instance constructor before trying to set up a watcher.',
                    )
                ):
                    self.param.trigger('x')

                with pytest.raises(
                    param._utils.ParamFutureWarning,
                    match=re.escape(
                        '(Un)registering a watcher on a partially initialized Parameterized instance '
                        'is deprecated and will raise an error in a future version. Ensure '
                        'you have called super().__init__(**) in the Parameterized instance '
                        'constructor before trying to set up a watcher.',
                    )
                ):
                    self.param.watch(print, 'x')

                self.param.objects(instance=False)
                super().__init__(**params)

        P()

    # Inheritance tests to be move to testparameterizedobject.py when warnings will be turned into errors

    def test_inheritance_with_incompatible_defaults(self):
        class A(param.Parameterized):
            p = param.String()

        class B(A): pass

        with pytest.raises(
            param._utils.ParamFutureWarning,
            match=re.escape(
                "Number parameter 'C.p' failed to validate its "
                "default value on class creation, this is going to raise "
                "an error in the future. The Parameter type changed between class 'C' "
                "and one of its parent classes (B, A) which made it invalid. "
                "Please fix the Parameter type."
                "\nValidation failed with:\nNumber parameter 'C.p' only takes numeric values, not <class 'str'>."
            )
        ):
            class C(B):
                p = param.Number()

    def test_inheritance_default_validation_with_more_specific_type(self):
        class A(param.Parameterized):
            p = param.Tuple(default=('a', 'b'))

        class B(A): pass

        with pytest.raises(
            param._utils.ParamFutureWarning,
            match=re.escape(
                "NumericTuple parameter 'C.p' failed to validate its "
                "default value on class creation, this is going to raise "
                "an error in the future. The Parameter type changed between class 'C' "
                "and one of its parent classes (B, A) which made it invalid. "
                "Please fix the Parameter type."
                "\nValidation failed with:\nNumericTuple parameter 'C.p' only takes numeric values, not <class 'str'>."
            )
        ):
            class C(B):
                p = param.NumericTuple()

    def test_inheritance_with_changing_bounds(self):
        class A(param.Parameterized):
            p = param.Number(default=5)

        class B(A): pass

        with pytest.raises(
            param._utils.ParamFutureWarning,
            match=re.escape(
                "Number parameter 'C.p' failed to validate its "
                "default value on class creation, this is going to raise "
                "an error in the future. The Parameter is defined with attributes "
                "which when combined with attributes inherited from its parent "
                "classes (B, A) make it invalid. Please fix the Parameter attributes."
                "\nValidation failed with:\nNumber parameter 'C.p' must be at most 3, not 5."
            )
        ):
            class C(B):
                p = param.Number(bounds=(-1, 3))

    def test_inheritance_with_changing_default(self):
        class A(param.Parameterized):
            p = param.Number(default=5, bounds=(3, 10))

        class B(A): pass

        with pytest.raises(
            param._utils.ParamFutureWarning,
            match=re.escape(
                "Number parameter 'C.p' failed to validate its "
                "default value on class creation, this is going to raise "
                "an error in the future. The Parameter is defined with attributes "
                "which when combined with attributes inherited from its parent "
                "classes (B, A) make it invalid. Please fix the Parameter attributes."
                "\nValidation failed with:\nNumber parameter 'C.p' must be at least 3, not 1."
            )
        ):
            class C(B):
                p = param.Number(default=1)

    def test_inheritance_with_changing_class_(self):
        class A(param.Parameterized):
            p = param.ClassSelector(class_=int, default=5)

        class B(A): pass

        with pytest.raises(
            param._utils.ParamFutureWarning,
            match=re.escape(
                "ClassSelector parameter 'C.p' failed to validate its "
                "default value on class creation, this is going to raise "
                "an error in the future. The Parameter is defined with attributes "
                "which when combined with attributes inherited from its parent "
                "classes (B, A) make it invalid. Please fix the Parameter attributes."
                "\nValidation failed with:\nClassSelector parameter 'C.p' value must be an instance of str, not 5."
            )
        ):
            class C(B):
                p = param.ClassSelector(class_=str)

class TestDeprecateParameters:

    def test_deprecate_print_param_defaults(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.print_param_defaults()

    def test_deprecate_set_default(self):
        class P(param.Parameterized):
            x = param.Parameter()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            P.param.set_default('x', 1)

    def test_deprecate__add_parameter(self):
        class P(param.Parameterized):
            x = param.Parameter()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            P.param._add_parameter('y', param.Parameter())

    def test_deprecate_params(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.params()

    def test_deprecate_set_param(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.set_param('x', 1)

    def test_deprecate_get_param_values(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.get_param_values()

    def test_deprecate_params_depended_on(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.params_depended_on()

    def test_deprecate_defaults(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.defaults()

    def test_deprecate_print_param_values(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.print_param_values()

    def test_deprecate_message(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.message('test')

    def test_deprecate_verbose(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.verbose('test')

    def test_deprecate_debug(self):
        class P(param.Parameterized):
            x = param.Parameter()

        p = P()

        with pytest.raises(param._utils.ParamDeprecationWarning):
            p.param.debug('test')