Skip to content

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

Merged
27Bslash6 merged 4 commits into
mainfrom
feat/pythonic-serializer-tuple
May 18, 2026
Merged

feat: add tuple preservation to AutoSerializer and 'pythonic' alias#121
27Bslash6 merged 4 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 that uses the same integrity-checking behavior as core serializers.
  • Bug Fixes

    • Tuple support added to serialization: tuples (including nested, empty, single-element, and when nested in lists/dicts) are preserved through roundtrips; malformed tuple markers now raise serialization errors.
  • Tests / Documentation

    • Tests and docs 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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b91d02a1-cf10-4007-8a6c-894e047a3a48

📥 Commits

Reviewing files that changed from the base of the PR and between db3583e and 5512b36.

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

📝 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 | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning Description covers Summary, Motivation, and Test Plan but is missing the Type of Change checkbox selection and most Security/Documentation/Testing/Backward Compatibility checklists required by template. Complete the required template sections: select Type of Change, verify Security Checklist items, confirm Testing checklist, and document Backward Compatibility assessment.
Docstring Coverage ⚠️ Warning Docstring coverage is 61.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately summarizes the two main changes: tuple preservation in AutoSerializer and addition of 'pythonic' alias, both clearly related to the changeset.
Linked Issues check ✅ Passed All code changes fully address linked issue #78 objectives: tuple preservation via type markers is implemented, 'pythonic' alias is registered, and comprehensive tests verify both features.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: registry alias addition, tuple marker serialization/deserialization logic, and comprehensive tuple-focused tests. No extraneous modifications detected.

✏️ 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 thread tests/unit/test_auto_serializer_new_types.py
@27Bslash6 27Bslash6 merged commit 4ab03d8 into main May 18, 2026
32 checks passed
@27Bslash6 27Bslash6 deleted the feat/pythonic-serializer-tuple branch May 18, 2026 11:37
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