Skip to content

Make RedisType instances picklable #228

Description

@yedidyakfir

Summary

RedisType instances (RedisStr, RedisInt, RedisFloat, RedisBytes, RedisList, RedisDict) should be picklable so they can be used as values within arbitrary container types like dict[str, Any], list[Any], or any other serialization context that relies on pickle.

Currently, pickling a RedisType instance fails because it holds a _base_model_link reference to the parent AtomicRedisModel, which in turn contains unpicklable Redis client/connection objects.

Use Cases

  • Storing RedisType values inside a dict[str, Any] field that gets serialized via pickle
  • Passing RedisType instances across process boundaries (e.g., multiprocessing)
  • Any generic serialization workflow that expects values to be picklable

Features / Details

  • Implement __getstate__ / __setstate__ on BaseRedisType to handle pickling
  • Exclude unpicklable references (_base_model_link and transitive Redis client/connection objects) during __getstate__
  • Restore or reset state appropriately in __setstate__
  • Handle container types (RedisList, RedisDict) which recursively hold child RedisType items with parent references
  • Preserve the actual value (the underlying str, int, float, bytes, list, dict) through the pickle round-trip

Implementation Considerations

  • _base_model_link creates circular references and holds unpicklable Redis connections — it must be excluded or replaced during pickling
  • After unpickling, _base_model_link will be None — the unpickled instance is a "detached" value, no longer connected to Redis
  • Container types (RedisList, RedisDict) need to recursively handle child items' _base_model_link references
  • Context-tracking fields like _redis_updated and _last_action_groups should either be preserved or reset to defaults on unpickling

List tasks

  • Implement __getstate__ and __setstate__ on BaseRedisType to exclude _base_model_link and related unpicklable objects
  • Override pickle methods on RedisList and RedisDict to handle recursive child field references
  • Add unit tests verifying pickle round-trip for each RedisType (RedisStr, RedisInt, RedisFloat, RedisBytes, RedisList, RedisDict)
  • Test pickling RedisType values nested inside dict[str, Any] and list[Any] containers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions