Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __replace__ magic method to BaseContainer for copy.replace() support #2093

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AdrianAcala
Copy link

@AdrianAcala AdrianAcala commented Apr 2, 2025

  • Implemented the replace method in BaseContainer to allow for the creation of new container instances with modified internal values, in line with the copy.replace() function introduced in Python 3.13.
  • Updated documentation to reflect this new feature and provided usage examples.
  • Added tests to ensure the correct functionality of the replace method and its integration with the copy module.

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

🙏 Please, if you or your company finds dry-python valuable, help us sustain the project by sponsoring it transparently on https://github.com/sponsors/dry-python. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.

Sorry, something went wrong.

@AdrianAcala AdrianAcala force-pushed the 1920-support-copy-replace-for-3-13 branch 3 times, most recently from 5e23f16 to 3169b17 Compare April 2, 2025 00:58
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

assert copy.replace(container) is container # type: ignore[attr-defined]

new_value = 'new_value'
new_container = copy.replace(container, _inner_value=new_value) # type: ignore[attr-defined]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with copy.replace is that it breaks typing :(

It does not know that copy.replace(Success(1), 'a') will be Result[str, Any] and not Result[int, Any]

We need a typed wrapper around it for our containers. With HKT support.

@AdrianAcala AdrianAcala force-pushed the 1920-support-copy-replace-for-3-13 branch 2 times, most recently from 41b4bfb to 288301e Compare April 3, 2025 02:09
@AdrianAcala AdrianAcala marked this pull request as draft April 3, 2025 02:43
@AdrianAcala AdrianAcala force-pushed the 1920-support-copy-replace-for-3-13 branch from 288301e to 77b5252 Compare April 3, 2025 03:48
Copy link
Author

@AdrianAcala AdrianAcala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sobolevn , thanks for your review. I'll try to fix things and get back to you.

Edit: sorry. I missed something.

@AdrianAcala AdrianAcala marked this pull request as ready for review April 3, 2025 03:50
@AdrianAcala AdrianAcala force-pushed the 1920-support-copy-replace-for-3-13 branch 2 times, most recently from c515492 to 9c764ce Compare April 3, 2025 04:00
@AdrianAcala AdrianAcala marked this pull request as draft April 3, 2025 04:06
@AdrianAcala AdrianAcala force-pushed the 1920-support-copy-replace-for-3-13 branch 2 times, most recently from 76db12c to 0ed959b Compare April 3, 2025 04:30
@AdrianAcala AdrianAcala marked this pull request as ready for review April 3, 2025 04:32
@AdrianAcala AdrianAcala marked this pull request as draft April 3, 2025 08:24
Adrian Acala added 3 commits April 3, 2025 09:02

Verified

This commit was signed with the committer’s verified signature.
petk Peter Kokot
- Added WPS453 to the per-file ignores in setup.cfg for docs/conf.py.
- Updated docs/conf.py to include a noqa comment for WPS453, suppressing the warning in the documentation configuration file.
- Implemented the __replace__ method in BaseContainer to allow for the creation of new container instances with modified internal values, in line with the copy.replace() function introduced in Python 3.13.
- Updated documentation to reflect this new feature and provided usage examples.
- Added tests to ensure the correct functionality of the __replace__ method and its integration with the copy module.
- Updated CHANGELOG to reflect this new feature.
- Updated the documentation to clarify the usage of the __replace__ method and introduced examples for both pre- and post-Python 3.13 approaches.
- Refactored test cases to improve clarity and ensure they accurately validate the behavior of the __replace__ method and its integration with the copy module.
- Renamed test functions for consistency and better readability.
@AdrianAcala AdrianAcala force-pushed the 1920-support-copy-replace-for-3-13 branch from 0ed959b to 118084c Compare April 3, 2025 16:03
@AdrianAcala AdrianAcala marked this pull request as ready for review April 3, 2025 16:06
@AdrianAcala AdrianAcala marked this pull request as draft April 3, 2025 16:07
>>> from returns.result import Success
>>>
>>> value = Success(1)
>>> # We can use map to effectively replace the inner value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that's not correct :(
This way we can only replace values in "truthy" values. It will not replace a value in Failure(1), for example.

@@ -299,121 +336,4 @@ We can also change the initial element to some other value:
... sum_two_numbers,
... ) == IO(25)

``Fold.loop`` is eager. It will be executed for all items in your iterable.

Collecting an iterable of containers into a single container
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this part?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Support copy.replace from 3.13
2 participants