feat: add __replace__ to BaseContainer for copy.replace support#2345
Closed
TheAbMehta wants to merge 2 commits intodry-python:masterfrom
Closed
feat: add __replace__ to BaseContainer for copy.replace support#2345TheAbMehta wants to merge 2 commits intodry-python:masterfrom
__replace__ to BaseContainer for copy.replace support#2345TheAbMehta wants to merge 2 commits intodry-python:masterfrom
Conversation
…on 3.13+) Add the __replace__ magic method to BaseContainer so that copy.replace(container, inner_value=new_val) creates a new container of the same type with the given inner value. The method accepts a single keyword argument 'inner_value' and raises TypeError for any unrecognised fields, following the copy.replace protocol introduced in Python 3.13 (PEP 618). Tests cover the dunder method directly (all Python versions), the copy.replace integration (skipped below 3.13), no-arg identity, and the rejection of unknown fields. Closes #1920 (partial — CI matrix already includes 3.13)
- Remove explicit positional index in format string (UP030) - Avoid direct dunder calls in tests by using a bound reference (PLC2801)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
__replace__magic method toBaseContainerso that Python 3.13'scopy.replace()works with all container types (Maybe,Result,IO, etc.).Details
Python 3.13 introduced
copy.replace()(PEP 618, docs) which delegates to__replace__(**kwargs)on the target object. This PR adds that protocol toBaseContainer.Usage:
Implementation:
inner_valueas the only valid keyword argumentTypeErrorfor unrecognised field names__replace__()directly;copy.replace()integration requires 3.13+Tests:
__replace__with a new value__replace__with no kwargs (identity)copy.replaceintegration (skipped below 3.13)The CI matrix already includes Python 3.13, so that part of #1920 is already done.
Closes #1920
Disclosure
This PR was authored with the assistance of Claude (LLM) to help understand the codebase and structure the implementation and description.