Skip to content

feat: add tuple preservation to AutoSerializer and 'pythonic' alias#121

Open
27Bslash6 wants to merge 3 commits into
mainfrom
feat/pythonic-serializer-tuple
Open

feat: add tuple preservation to AutoSerializer and 'pythonic' alias#121
27Bslash6 wants to merge 3 commits into
mainfrom
feat/pythonic-serializer-tuple

Conversation

@27Bslash6
Copy link
Copy Markdown
Contributor

@27Bslash6 27Bslash6 commented May 16, 2026

Summary

  • AutoSerializer now preserves tuples through serialization roundtrips (matching existing set/frozenset/datetime/UUID preservation)
  • Add serializer='pythonic' as a registry alias for 'auto'

Why

AutoSerializer preserved sets and frozensets via type markers but not tuples — inconsistent. The tricky part: msgpack natively serializes tuples as arrays (so the default callback is never called). Fixed by pre-processing tuples into {"__tuple__": True, "value": [...]} markers before encoding.

Test plan

  • Tuple roundtrip (simple, nested, mixed with sets/datetimes)
  • get_serializer('pythonic') returns AutoSerializer
  • 1405 unit tests pass
  • Lint + type check clean

Closes #78

Summary by CodeRabbit

  • New Features

    • Added a "pythonic" serializer alias with the same integrity-checking behavior as other core serializers.
  • Bug Fixes

    • Tuple support added to serialization: tuples (including nested and empty) are preserved through roundtrips and when nested inside lists/dicts.
  • Tests / Documentation

    • Tests and documentation updated to cover tuple handling and the "pythonic" alias.

Review Change Stack

AutoSerializer now preserves tuples through serialization roundtrips
via type markers, matching the existing set/frozenset/datetime/UUID
preservation. Add 'pythonic' as a registry alias for 'auto' to better
communicate intent.

Implementation: tuples are pre-processed into {"__tuple__": True,
"value": [...]} markers before msgpack encoding (since msgpack natively
flattens tuples to arrays). The object_hook restores them on decode.

Closes #78
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 16, 2026

📝 Walkthrough

Walkthrough

AutoSerializer now preserves tuples by wrapping them as explicit tuple markers before MessagePack packing and reconstructs them on unpacking. The serializer registry gains a "pythonic" alias mapped to AutoSerializer and instantiated with integrity checking like other core serializers.

Changes

AutoSerializer Tuple Preservation & Pythonic Alias

Layer / File(s) Summary
Pythonic serializer alias registration
src/cachekit/serializers/__init__.py
SERIALIZER_REGISTRY adds the "pythonic": AutoSerializer entry and get_serializer includes "pythonic" in the integrity-checking instantiation branch; tests verify the alias returns an AutoSerializer.
Tuple serialization and deserialization
src/cachekit/serializers/auto_serializer.py, tests/unit/test_auto_serializer_new_types.py
Added _wrap_tuples to convert tuples (including nested) into {"__tuple__": True, "value": [...]} before msgpack.packb; extended _auto_object_hook to validate and reconstruct tuples; documentation/error messages and tests updated to cover tuple roundtrips and malformed marker errors.

🎯 3 (Moderate) | ⏱️ ~20 minutes

"I'm a rabbit with a tiny pen,
Tuples hop back safe again.
Wrapped in markers snug and bright,
Pythonic alias joins the flight.
Hooray — roundtrips sleep at night!" 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.71% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the two main changes: adding tuple preservation to AutoSerializer and adding the 'pythonic' alias.
Description check ✅ Passed The description covers motivation, implementation details, and test validation. It follows the template structure with Summary, Why, and Test plan sections addressing the key requirements.
Linked Issues check ✅ Passed The pull request fulfills both objectives from issue #78: adds 'pythonic' as a registry alias for AutoSerializer and implements tuple preservation through markers in encoding/decoding.
Out of Scope Changes check ✅ Passed All changes are directly related to the linked issue objectives: registry alias addition, tuple marker implementation in AutoSerializer, and comprehensive test coverage for both features.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/pythonic-serializer-tuple

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

27Bslash6 added 2 commits May 16, 2026 22:11
10 tests covering: simple/nested/empty tuple roundtrips, tuples in
dicts and lists, tuples with other special types (sets, datetimes),
list-stays-list verification, malformed marker error paths, and
pythonic registry alias.
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/unit/test_auto_serializer_new_types.py (1)

564-646: 💤 Low value

Consider adding single-element tuple test.

Single-element tuples like (1,) are a common Python edge case. While the current implementation likely handles them correctly, adding a test would provide explicit coverage for this pattern.

📝 Optional test to add
def test_single_element_tuple_roundtrip(self):
    """Single-element tuples preserve the trailing comma semantics."""
    serializer = AutoSerializer()
    data = (1,)
    serialized, metadata = serializer.serialize(data)
    result = serializer.deserialize(serialized, metadata)
    assert isinstance(result, tuple)
    assert len(result) == 1
    assert result == (1,)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/test_auto_serializer_new_types.py` around lines 564 - 646, Add a
new unit test method test_single_element_tuple_roundtrip to the
TestAutoSerializerTuple class that constructs serializer = AutoSerializer(),
sets data = (1,), calls serializer.serialize(data) and
serializer.deserialize(serialized, metadata), and asserts the result is a tuple,
has length 1, and equals (1,); this provides explicit coverage for
single-element tuple semantics alongside the existing tuple tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/unit/test_auto_serializer_new_types.py`:
- Around line 629-645: Update the two tests
test_malformed_tuple_marker_missing_value and
test_malformed_tuple_marker_wrong_value_type to remove the unnecessary
enable_integrity_checking=False argument when instantiating AutoSerializer; the
tuple validation runs in _auto_object_hook during msgpack deserialization and is
independent of ByteStorage integrity/compression, so change "serializer =
AutoSerializer(enable_integrity_checking=False)" to "serializer =
AutoSerializer()" in both tests to match the pattern used by other
corruption/roundtrip tests.

---

Nitpick comments:
In `@tests/unit/test_auto_serializer_new_types.py`:
- Around line 564-646: Add a new unit test method
test_single_element_tuple_roundtrip to the TestAutoSerializerTuple class that
constructs serializer = AutoSerializer(), sets data = (1,), calls
serializer.serialize(data) and serializer.deserialize(serialized, metadata), and
asserts the result is a tuple, has length 1, and equals (1,); this provides
explicit coverage for single-element tuple semantics alongside the existing
tuple tests.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 769641c7-1a80-44b6-8b25-77931f52380b

📥 Commits

Reviewing files that changed from the base of the PR and between 67f37d2 and db3583e.

📒 Files selected for processing (2)
  • src/cachekit/serializers/auto_serializer.py
  • tests/unit/test_auto_serializer_new_types.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/cachekit/serializers/auto_serializer.py

Comment on lines +629 to +645
def test_malformed_tuple_marker_missing_value(self):
"""Malformed __tuple__ marker raises SerializationError."""
import msgpack

serializer = AutoSerializer(enable_integrity_checking=False)
bad_data = msgpack.packb({"__tuple__": True})
with pytest.raises(SerializationError, match="missing 'value' field"):
serializer.deserialize(bad_data)

def test_malformed_tuple_marker_wrong_value_type(self):
"""__tuple__ marker with non-list value raises SerializationError."""
import msgpack

serializer = AutoSerializer(enable_integrity_checking=False)
bad_data = msgpack.packb({"__tuple__": True, "value": "not a list"})
with pytest.raises(SerializationError, match="expected list"):
serializer.deserialize(bad_data)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check if enable_integrity_checking=False is necessary in tuple corruption tests

# Search for all corruption tests with enable_integrity_checking
rg -n "enable_integrity_checking" tests/unit/test_auto_serializer_new_types.py -A 2 -B 2

# Search for corruption test patterns to compare tuple vs other types
rg -n "def test_.*_missing_value_field" tests/unit/test_auto_serializer_new_types.py -A 5

Repository: cachekit-io/cachekit-py

Length of output: 2364


🏁 Script executed:

# Find AutoSerializer class definition
fd AutoSerializer src/ --type f | head -20

Repository: cachekit-io/cachekit-py

Length of output: 49


🏁 Script executed:

# Search for enable_integrity_checking usage and documentation
rg -n "enable_integrity_checking" --type py | head -40

Repository: cachekit-io/cachekit-py

Length of output: 4796


🏁 Script executed:

# Search for tuple deserialization logic
rg -n "__tuple__" --type py -A 3 -B 3 | head -60

Repository: cachekit-io/cachekit-py

Length of output: 3924


🏁 Script executed:

# Check ByteStorage code and its interaction with deserialization
rg -n "ByteStorage" src/cachekit/serializers/auto_serializer.py -B 2 -A 5 | head -50

Repository: cachekit-io/cachekit-py

Length of output: 1615


🏁 Script executed:

# Check if there's any special handling of integrity checking in deserialization flow
rg -n "_auto_object_hook|__tuple__" src/cachekit/serializers/auto_serializer.py -B 3 -A 8 | head -80

Repository: cachekit-io/cachekit-py

Length of output: 2636


🏁 Script executed:

# Check if any other test file tests tuple with or without the flag
rg -n "test_.*tuple" tests/unit/ -A 2 -B 1 | grep -E "(def test|enable_integrity)" | head -20

Repository: cachekit-io/cachekit-py

Length of output: 1296


Remove unnecessary enable_integrity_checking=False flag for consistency.

Both tuple corruption tests use enable_integrity_checking=False, but this flag controls ByteStorage compression/integrity wrapper behavior—not tuple marker validation. The tuple validation (checking for missing 'value' field and correct value type) occurs in _auto_object_hook during msgpack deserialization, independent of this flag. All other corruption tests (UUID, datetime, date, time, set, frozenset) and all other tuple roundtrip tests omit this flag. Remove it from both test methods to align with the pattern used throughout the file.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/test_auto_serializer_new_types.py` around lines 629 - 645, Update
the two tests test_malformed_tuple_marker_missing_value and
test_malformed_tuple_marker_wrong_value_type to remove the unnecessary
enable_integrity_checking=False argument when instantiating AutoSerializer; the
tuple validation runs in _auto_object_hook during msgpack deserialization and is
independent of ByteStorage integrity/compression, so change "serializer =
AutoSerializer(enable_integrity_checking=False)" to "serializer =
AutoSerializer()" in both tests to match the pattern used by other
corruption/roundtrip tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add 'pythonic' serializer alias and tuple preservation

1 participant