Skip to content

Add __replace__ and fix __reduce__ for structseq#6978

Merged
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:structseq
Feb 3, 2026
Merged

Add __replace__ and fix __reduce__ for structseq#6978
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:structseq

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 3, 2026

  • Add __replace__ method to PyStructSequence trait
  • Move __reduce__ from #[pymethod] to extend_pyclass with contains_key guard, allowing per-type overrides
  • Fix repr: remove trailing comma for single-field sequences

Summary by CodeRabbit

  • New Features

    • Added __replace__ method for struct sequences, enabling creation of modified copies by replacing selected fields via keyword arguments.
    • Improved default serialization support for struct sequences with automatic __reduce__ method handling.
  • Bug Fixes

    • Enhanced field representation logic for improved consistency in struct sequence formatting.

- Add __replace__ method to PyStructSequence trait
- Move __reduce__ from #[pymethod] to extend_pyclass with
  contains_key guard, allowing per-type overrides
- Fix repr: remove trailing comma for single-field sequences
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 3, 2026

📝 Walkthrough

Walkthrough

This change adds a new __replace__ method to struct sequences, enabling field value modification via kwargs while reconstructing instances. A default __reduce__ method is introduced for pickle serialization support, and the representation logic is refactored to use uniform field iteration instead of single-field optimization.

Changes

Cohort / File(s) Summary
Struct Sequence Enhancements
crates/vm/src/types/structseq.rs
Introduced DEFAULT_STRUCTSEQ_REDUCE constant and __replace__ method for struct sequences with field validation. Extended class initialization to auto-attach the default reduce method. Reworked repr creation to use generalized field-iteration path, removing single-field optimization. Updated imports to include PyMethodDef, PyMethodFlags, and FuncArgs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • Complete tp_repr #5852: Modifies structseq.rs's representation code and repr/slot_repr implementation, affecting the same field-formatting paths.

Poem

🐰 A tuple's fields now dance and play,
With __replace__ showing them the way,
Old reduce defaults, no override needed,
A uniform repr path, at last succeeded! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: adding a replace method and fixing reduce for struct sequences, which aligns with the core objectives and file modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

Code has been automatically formatted

The code in this PR has been formatted using:

  • cargo fmt --all
    Please pull the latest changes before pushing again:
git pull origin structseq

@youknowone youknowone marked this pull request as ready for review February 3, 2026 09:56
@youknowone youknowone merged commit 023b3b2 into RustPython:main Feb 3, 2026
13 checks passed
@youknowone youknowone deleted the structseq branch February 3, 2026 10:09
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.

1 participant