Skip to content

Commit

Permalink
Add __replace__ magic method to Container base class
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Sunglasses committed Aug 29, 2024
1 parent d8fd5b7 commit 3723f67
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions returns/primitives/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def __setstate__(self, state: Union[_PickleState, Any]) -> None:
# backward compatibility with 0.19.0 and earlier
object.__setattr__(self, '_inner_value', state) # noqa: WPS609

def __replace__(self, **changes: Any) -> 'BaseContainer':
"""Create a new instance with specified changes."""
if set(changes.keys()) - set(self.__slots__):
raise ValueError("Invalid attribute(s) specified")
new_inner_value = changes.get('_inner_value', self._inner_value)
return type(self)(new_inner_value)

def container_equality(
self: Kind1[_EqualType, Any],
Expand Down

0 comments on commit 3723f67

Please sign in to comment.