Test that state serialization will fall back to dill.
()
| 4344 | |
| 4345 | |
| 4346 | def test_fallback_pickle(): |
| 4347 | """Test that state serialization will fall back to dill.""" |
| 4348 | |
| 4349 | class DillState(BaseState): |
| 4350 | _o: Obj | None = None |
| 4351 | _f: Callable | None = None |
| 4352 | _g: Any = None |
| 4353 | |
| 4354 | state = DillState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] |
| 4355 | state._o = Obj(f=lambda: 42) |
| 4356 | state._f = lambda: 420 |
| 4357 | |
| 4358 | pk = state._serialize() |
| 4359 | |
| 4360 | unpickled_state = BaseState._deserialize(pk) |
| 4361 | assert isinstance(unpickled_state, DillState) |
| 4362 | assert unpickled_state._f is not None |
| 4363 | assert unpickled_state._f() == 420 |
| 4364 | assert unpickled_state._o is not None |
| 4365 | assert unpickled_state._o.f() == 42 |
| 4366 | |
| 4367 | # Threading locks are unpicklable normally, and raise TypeError instead of PicklingError. |
| 4368 | state2 = DillState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] |
| 4369 | state2._g = threading.Lock() |
| 4370 | pk2 = state2._serialize() |
| 4371 | unpickled_state2 = BaseState._deserialize(pk2) |
| 4372 | assert isinstance(unpickled_state2, DillState) |
| 4373 | assert isinstance(unpickled_state2._g, type(threading.Lock())) |
| 4374 | |
| 4375 | # Some object, like generator, are still unpicklable with dill. |
| 4376 | state3 = DillState(_reflex_internal_init=True) # pyright: ignore [reportCallIssue] |
| 4377 | state3._g = (i for i in range(10)) |
| 4378 | |
| 4379 | with pytest.raises(StateSerializationError): |
| 4380 | _ = state3._serialize() |
| 4381 | |
| 4382 | |
| 4383 | def test_typed_state() -> None: |
nothing calls this directly
no test coverage detected