You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: implement __replace__ on BaseContainer for copy.replace (#1920) - #2568
Python 3.13 introduced copy.replace(obj, **changes) which relies on the __replace__() protocol method. Currently, BaseContainer does not define __replace__, preventing users from using copy.replace on containers like Result, Maybe, IO, etc.
Solution
Implemented __replace__(self: _ContainerType, **changes: Any) -> _ContainerType on BaseContainer in returns/primitives/container.py:
Accepts inner_value (the constructor parameter name) or _inner_value (the slot attribute name) and constructs a new instance of the same type.
Raises a descriptive TypeError if unexpected keyword arguments are provided or if both inner_value and _inner_value are specified.
Calling without arguments returns an equal copy (self.__class__(self._inner_value)).
Implemented specialized __replace__ on _Nothing in returns/maybe.py:
Returns self when inner_value is None.
Raises a TypeError if a non-None value is supplied to preserve the empty state semantics.
Error-handling assertions for unexpected keyword arguments and ambiguous parameter values.
Standard library copy.replace integration test (conditionally executed on Python 3.13+).
Verification
Tested with pytest --no-cov tests/test_primitives/test_container/test_base_container/test_replace.py:
10 passed, 1 skipped in 0.82s
Contributor Direct Contact
For direct communication regarding this contribution, technical questions, collaboration, or follow-up: Direct Contact / WhatsApp: +923167417135
Please feel free to contact me directly to discuss the implementation or coordinate further improvements.
Merging this PR will degrade performance by 10.59%
1 warning, 1 note
⚠️Base and head ran on different runner types
Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.
The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing hamid-digitalmarketer:feat/issue-1920-copy-replace-container (9664961) with master (ea76fb5)
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
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
Fixes #1920
Problem
Python 3.13 introduced
copy.replace(obj, **changes)which relies on the__replace__()protocol method. Currently,BaseContainerdoes not define__replace__, preventing users from usingcopy.replaceon containers likeResult,Maybe,IO, etc.Solution
__replace__(self: _ContainerType, **changes: Any) -> _ContainerTypeonBaseContainerinreturns/primitives/container.py:inner_value(the constructor parameter name) or_inner_value(the slot attribute name) and constructs a new instance of the same type.TypeErrorif unexpected keyword arguments are provided or if bothinner_valueand_inner_valueare specified.self.__class__(self._inner_value)).__replace__on_Nothinginreturns/maybe.py:selfwheninner_valueisNone.TypeErrorif a non-Nonevalue is supplied to preserve the empty state semantics.Tests Added
Added
tests/test_primitives/test_container/test_base_container/test_replace.py:Success,Failure,Some,IO,Nothing).copy.replaceintegration test (conditionally executed on Python 3.13+).Verification
pytest --no-cov tests/test_primitives/test_container/test_base_container/test_replace.py:Contributor Direct Contact
For direct communication regarding this contribution, technical questions, collaboration, or follow-up:
Direct Contact / WhatsApp: +923167417135
Please feel free to contact me directly to discuss the implementation or coordinate further improvements.