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
|
import asyncio
import threading
import time
import param
import pytest
from param.parameterized import Skip, resolve_ref
from param.reactive import bind, rx
class Parameters(param.Parameterized):
string = param.String(default="string", allow_refs=True)
dictionary = param.Dict(default={}, allow_refs=True, nested_refs=True)
string_list = param.List(default=[], item_type=str, allow_refs=True, nested_refs=True)
no_refs = param.Parameter(allow_refs=False)
@param.depends('string')
def formatted_string(self):
if self.string.endswith('?'):
raise Skip()
return self.string + '!'
@param.depends('string')
def formatted_string_skip_return(self):
if self.string.endswith('?'):
return Skip
return self.string + '!'
class Subclass(Parameters):
no_refs = param.Parameter()
class SubclassOverride(Parameters):
no_refs = param.Parameter(allow_refs=True)
class Nested(param.Parameterized):
subobject = param.ClassSelector(class_=Parameters)
@param.depends('subobject.string')
def string(self):
return self.subobject.string + '!'
def test_class_explicit_no_refs():
assert Parameters._param__private.explicit_no_refs == ['no_refs']
def test_subclass_explicit_no_refs():
assert Subclass._param__private.explicit_no_refs == ['no_refs']
def test_subclass_explicit_no_refs_override():
assert SubclassOverride._param__private.explicit_no_refs == []
def test_parameterized_warns_explicit_no_ref():
class ImplicitRefsParameters(param.Parameterized):
parameter = param.Parameter(default="string")
p = Parameters()
with pytest.raises(Exception) as e:
ImplicitRefsParameters(parameter=p.param.string)
assert f"Parameter 'parameter' on {ImplicitRefsParameters} is being given a valid parameter reference <param.parameterized.String" in str(e.value)
def test_parameter_ref():
p = Parameters()
p2 = Parameters(string=p.param.string)
assert p.string == p2.string
p.string = 'new_string'
assert p2.string == 'new_string'
def test_parameter_ref_update():
p = Parameters()
p2 = Parameters(string=p.param.string)
p3 = Parameters(string='new string')
p2.string = p3.param.string
assert p2.string == 'new string'
p.string = 'still linked'
assert p2.string != 'still linked'
p3.string = 'newly linked'
assert p2.string == 'newly linked'
def test_parameter_ref_update_context():
p = Parameters(string='linked')
p2 = Parameters(string=p.param.string)
assert p2.string == 'linked'
with p2.param.update(string='override'):
assert p2.string == 'override'
p.string = 'not yet linked'
assert p2.string == 'override'
assert p2.string == 'not yet linked'
p.string = 'relinked'
assert p2.string == 'relinked'
def test_bind_ref():
p = Parameters()
p2 = Parameters(string=bind(lambda x: x + '!', p.param.string))
assert p2.string == 'string!'
p.string = 'new string'
assert p2.string == 'new string!'
def test_bind_ref_skip():
p = Parameters()
def skip_fn(s):
if s == 'invalid':
raise Skip()
return s + '!'
p2 = Parameters(string=bind(skip_fn, p.param.string))
assert p2.string == 'string!'
p.string = 'new string'
assert p2.string == 'new string!'
p.string = 'invalid'
assert p2.string == 'new string!'
def test_reactive_ref():
string = rx('string')
rx_string = string+'!'
p = Parameters(string=rx_string)
assert p.string == 'string!'
string.rx.value = 'new string'
assert p.string == 'new string!'
def test_nested_list_parameter_ref():
p = Parameters()
p2 = Parameters(string_list=[p.param.string, 'other'])
p3 = Parameters(string='foo')
assert p2.string_list == ['string', 'other']
p2.string_list = [p3.param.string, 'another']
assert p2.string_list == ['foo', 'another']
def test_nested_dict_key_parameter_ref():
p = Parameters()
p2 = Parameters(dictionary={p.param.string: 'value'})
p3 = Parameters(string='foo')
assert p2.dictionary == {'string': 'value'}
p2.dictionary = {p3.param.string: 'new_value'}
assert p2.dictionary == {'foo': 'new_value'}
def test_nested_dict_value_parameter_ref():
p = Parameters()
p2 = Parameters(dictionary={'key': p.param.string})
p3 = Parameters(string='foo')
assert p2.dictionary == {'key': 'string'}
p2.dictionary = {'new key': p3.param.string}
assert p2.dictionary == {'new key': 'foo'}
def test_nested_param_method_ref():
p = Parameters()
nested = Nested(subobject=p)
p2 = Parameters(string=nested.string)
assert p2.string == 'string!'
p.string = 'new string'
assert p2.string == 'new string!'
def test_nested_param_method_skip():
p = Parameters()
p2 = Parameters(string=p.formatted_string)
assert p2.string == 'string!'
p.string = 'new string?'
assert p2.string == 'string!'
def test_nested_param_method_skip_return():
p = Parameters()
p2 = Parameters(string=p.formatted_string_skip_return)
assert p2.string == 'string!'
p.string = 'new string?'
assert p2.string == 'string!'
async def test_async_function_ref():
async def gen_strings():
await asyncio.sleep(0.02)
return 'string!'
p = Parameters(string=gen_strings)
await asyncio.sleep(0.1)
assert p.string == 'string!'
async def test_async_bind_ref():
p = Parameters()
async def exclaim(string):
await asyncio.sleep(0.05)
return string + '!'
p2 = Parameters(string=bind(exclaim, p.param.string))
await asyncio.sleep(0.1)
assert p2.string == 'string!'
p.string = 'new string'
await asyncio.sleep(0.1)
assert p2.string == 'new string!'
async def test_async_generator_ref():
async def gen_strings():
yield 'string?'
await asyncio.sleep(0.02)
yield 'string!'
p = Parameters(string=gen_strings)
await asyncio.sleep(0.01)
assert p.string == 'string?'
await asyncio.sleep(0.1)
assert p.string == 'string!'
async def test_generator_ref():
def gen_strings():
yield 'string?'
time.sleep(0.05)
yield 'string!'
p = Parameters(string=gen_strings)
await asyncio.sleep(0.02)
assert p.string == 'string?'
for _ in range(5):
await asyncio.sleep(0.05)
if p.string == 'string!':
break
assert p.string == 'string!'
async def test_async_generator_ref_cancelled():
tasks = []
async def gen_strings1():
tasks.append(asyncio.current_task())
i = 0
while True:
yield str(i)
await asyncio.sleep(0.01)
async def gen_strings2():
tasks.append(asyncio.current_task())
i = 0
while True:
yield str(i)
await asyncio.sleep(0.01)
p = Parameters(string=gen_strings1)
await asyncio.sleep(0.02)
assert p.string is not None
p.string = gen_strings2
await asyncio.sleep(0.02)
assert len(tasks) == 2
task1, task2 = tasks
assert task1.done()
assert not task2.done()
assert p._param__private.async_refs['string'] is task2
async def test_generator_ref_cancelled():
threads = []
def gen_strings1():
threads.append(threading.current_thread())
i = 0
while True:
yield str(i)
time.sleep(0.01)
def gen_strings2():
threads.append(threading.current_thread())
i = 0
while True:
yield str(i)
time.sleep(0.01)
p = Parameters(string=gen_strings1)
await asyncio.sleep(0.02)
task1 = p._param__private.async_refs['string']
assert p.string is not None
p.string = gen_strings2
await asyncio.sleep(0.02)
task2 = p._param__private.async_refs['string']
assert task1 is not task2
assert task1.done()
assert not task2.done()
assert len(threads) == 2
def test_resolve_ref_parameter():
p = Parameters()
refs = resolve_ref(p.param.string)
assert len(refs) == 1
assert refs[0] is p.param.string
def test_resolve_ref_depends_method():
p = Parameters()
refs = resolve_ref(p.formatted_string)
assert len(refs) == 1
assert refs[0] is p.param.string
def test_resolve_ref_recursive_list():
p = Parameters()
nested = [[p.param.string]]
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string
def test_resolve_ref_recursive_set():
p = Parameters()
nested = {(p.param.string,)} # Parameters aren't hashable
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string
def test_resolve_ref_recursive_tuple():
p = Parameters()
nested = ((p.param.string,),)
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string
def test_resolve_ref_recursive_dict():
p = Parameters()
nested = {'0': {'0': p.param.string}}
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string
def test_resolve_ref_recursive_slice():
p = Parameters()
nested = [slice(p.param.string)]
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string
|