Add __replace__ and fix __reduce__ for structseq by youknowone · Pull Request #6978 · RustPython/RustPython
📝 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.
Comment @coderabbitai help to get the list of available commands and usage tips.