Skip to content

Conversation

@AdrianAcala
Copy link

@AdrianAcalaAdrianAcala 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.

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

@sobolevnsobolevn left a comment

Choose a reason for hiding this comment

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

Thanks!

assertcopy.replace(container) iscontainer# 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.

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

@AdrianAcalaAdrianAcala 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.

@AdrianAcalaAdrianAcala marked this pull request as ready for review April 3, 2025 03:50
@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch 2 times, most recently from c515492 to 9c764ceCompareApril 3, 2025 04:00
@AdrianAcalaAdrianAcala marked this pull request as draft April 3, 2025 04:06
@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch 2 times, most recently from 76db12c to 0ed959bCompareApril 3, 2025 04:30
@AdrianAcalaAdrianAcala marked this pull request as ready for review April 3, 2025 04:32
@AdrianAcalaAdrianAcala marked this pull request as draft April 3, 2025 08:24
@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch from 0ed959b to 118084cCompareApril 3, 2025 16:03
@AdrianAcalaAdrianAcala marked this pull request as ready for review April 3, 2025 16:06
@AdrianAcalaAdrianAcala 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.

@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch from 118084c to 2fa9fdaCompareApril 14, 2025 19:34
@AdrianAcala
Copy link
Author

@sobolevn , sorry about the delay. My family got sick and then I got sick and then work has been busy since I missed some time. I went through this again and resolved what I think is what you want. Please review again please.

@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch 4 times, most recently from 828b34c to 3b9768dCompareApril 14, 2025 20:45
@AdrianAcalaAdrianAcala marked this pull request as ready for review April 14, 2025 20:47
@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch 2 times, most recently from 69cab3c to c68ea9fCompareApril 14, 2025 21:14
@AdrianAcalaAdrianAcala marked this pull request as draft April 14, 2025 21:32
…) support - Added the __replace__ magic method to BaseContainer, enabling the creation of modified copies of immutable containers in line with Python 3.13's copy.replace() functionality. - Updated documentation to include usage examples and clarify the behavior of the new method. - 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 and its implications for container usage. Closesdry-python#1920.
@AdrianAcalaAdrianAcalaforce-pushed the 1920-support-copy-replace-for-3-13 branch from c68ea9f to 51936c3CompareApril 19, 2025 02:05
@AdrianAcalaAdrianAcala marked this pull request as ready for review April 19, 2025 02:10
@AdrianAcala
Copy link
Author

@sobolevn , this is ready for your review. Thanks so much!

Sign up for freeto 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

@AdrianAcala@sobolevn