MCPcopy
hub / github.com/reflex-dev/reflex / test_fallback_pickle

Function test_fallback_pickle

tests/units/test_state.py:4346–4380  ·  view source on GitHub ↗

Test that state serialization will fall back to dill.

()

Source from the content-addressed store, hash-verified

4344
4345
4346def 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
4383def test_typed_state() -> None:

Callers

nothing calls this directly

Calls 4

DillStateClass · 0.85
ObjClass · 0.85
_serializeMethod · 0.80
_deserializeMethod · 0.80

Tested by

no test coverage detected