|
| 1 | +import sys |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +# Skip all tests if Python version is less than 3.13 |
| 6 | +if sys.version_info >= (3, 13): |
| 7 | + import copy |
| 8 | + |
| 9 | +from typing import Generic, TypeVar |
| 10 | + |
| 11 | +from returns.io import IO |
| 12 | +from returns.maybe import Nothing, Some |
| 13 | +from returns.primitives.container import BaseContainer |
| 14 | +from returns.result import Failure, Success |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.skipif( |
| 18 | + sys.version_info < (3, 13), |
| 19 | + reason='copy.replace requires Python 3.13+', |
| 20 | +) |
| 21 | +class TestCopyReplace: |
| 22 | + """Tests for copy.replace functionality added in Python 3.13.""" |
| 23 | + |
| 24 | + def test_success_copy_replace(self): |
| 25 | + """Tests copy.replace() on Success container.""" |
| 26 | + original = Success(42) |
| 27 | + replaced = copy.replace(original, _inner_value=100) |
| 28 | + |
| 29 | + assert replaced == Success(100) |
| 30 | + assert replaced is not original |
| 31 | + assert replaced._inner_value == 100 # noqa: SLF001 |
| 32 | + |
| 33 | + # No changes should return the same object (or equivalent) |
| 34 | + copied = copy.replace(original) |
| 35 | + assert copied == original |
| 36 | + # Unchanged objects return self per immutability principle |
| 37 | + assert copied is original |
| 38 | + |
| 39 | + def test_failure_copy_replace(self): |
| 40 | + """Tests copy.replace() on Failure container.""" |
| 41 | + original = Failure('error') |
| 42 | + replaced = copy.replace(original, _inner_value='new error') |
| 43 | + |
| 44 | + assert replaced == Failure('new error') |
| 45 | + assert replaced is not original |
| 46 | + assert replaced._inner_value == 'new error' # noqa: SLF001 |
| 47 | + |
| 48 | + # No changes should return an equivalent object |
| 49 | + copied = copy.replace(original) |
| 50 | + assert copied == original |
| 51 | + # Unchanged objects return self per immutability principle |
| 52 | + assert copied is original |
| 53 | + |
| 54 | + def test_some_copy_replace(self): |
| 55 | + """Tests copy.replace() on Some container.""" |
| 56 | + original = Some('value') |
| 57 | + replaced = copy.replace(original, _inner_value='new value') |
| 58 | + |
| 59 | + assert replaced == Some('new value') |
| 60 | + assert replaced is not original |
| 61 | + assert replaced._inner_value == 'new value' # noqa: SLF001 |
| 62 | + |
| 63 | + # No changes should return an equivalent object |
| 64 | + copied = copy.replace(original) |
| 65 | + assert copied == original |
| 66 | + # Unchanged objects return self per immutability principle |
| 67 | + assert copied is original |
| 68 | + |
| 69 | + def test_nothing_copy_replace(self): |
| 70 | + """Tests copy.replace() on Nothing singleton.""" |
| 71 | + # Note: Nothing is a singleton, and its constructor ignores the |
| 72 | + # passed value. _Nothing will just return Nothing in any case |
| 73 | + replaced = copy.replace(Nothing, _inner_value='something') |
| 74 | + |
| 75 | + assert replaced is Nothing |
| 76 | + assert replaced._inner_value is None # noqa: SLF001 |
| 77 | + |
| 78 | + def test_io_copy_replace(self): |
| 79 | + """Tests copy.replace() on IO container.""" |
| 80 | + original = IO('data') |
| 81 | + replaced = copy.replace(original, _inner_value='new data') |
| 82 | + |
| 83 | + assert replaced == IO('new data') |
| 84 | + assert replaced is not original |
| 85 | + assert replaced._inner_value == 'new data' # noqa: SLF001 |
| 86 | + |
| 87 | + def test_type_change_with_replace(self): |
| 88 | + """Tests copy.replace() when replacing with a different type.""" |
| 89 | + # Test with Success container |
| 90 | + int_success = Success(42) |
| 91 | + str_success = copy.replace(int_success, _inner_value='forty-two') |
| 92 | + |
| 93 | + assert str_success == Success('forty-two') |
| 94 | + assert str_success is not int_success |
| 95 | + assert isinstance(str_success._inner_value, str) # noqa: SLF001 |
| 96 | + |
| 97 | + # Test with Failure container |
| 98 | + str_failure = Failure('error') |
| 99 | + int_failure = copy.replace(str_failure, _inner_value=404) |
| 100 | + |
| 101 | + assert int_failure == Failure(404) |
| 102 | + assert int_failure is not str_failure |
| 103 | + assert isinstance(int_failure._inner_value, int) # noqa: SLF001 |
| 104 | + |
| 105 | + # Test with IO container |
| 106 | + list_io = IO([1, 2, 3]) |
| 107 | + dict_io = copy.replace(list_io, _inner_value={'a': 1, 'b': 2}) |
| 108 | + |
| 109 | + assert dict_io == IO({'a': 1, 'b': 2}) |
| 110 | + assert dict_io is not list_io |
| 111 | + assert isinstance(dict_io._inner_value, dict) # noqa: SLF001 |
| 112 | + |
| 113 | + def test_invalid_arguments_in_replace(self): |
| 114 | + """Tests that invalid arguments to copy.replace() raise TypeError.""" |
| 115 | + original = Success(42) |
| 116 | + |
| 117 | + with pytest.raises(TypeError) as exc_info: |
| 118 | + # The only valid argument for BaseContainer is _inner_value |
| 119 | + copy.replace(original, invalid_arg=True) |
| 120 | + |
| 121 | + error_message = str(exc_info.value) |
| 122 | + assert 'received unexpected arguments' in error_message |
| 123 | + assert 'invalid_arg' in error_message |
| 124 | + |
| 125 | + # Test with multiple invalid arguments |
| 126 | + with pytest.raises(TypeError): |
| 127 | + copy.replace(original, invalid_arg1=True, invalid_arg2='test') |
| 128 | + |
| 129 | + # Valid argument should work normally |
| 130 | + assert copy.replace(original, _inner_value=100) == Success(100) |
| 131 | + |
| 132 | + def test_custom_container(self): |
| 133 | + """Tests copy.replace() works with custom user-defined containers.""" |
| 134 | + T = TypeVar('T') |
| 135 | + |
| 136 | + # Define a simple custom container extending BaseContainer |
| 137 | + class CustomBox(BaseContainer, Generic[T]): |
| 138 | + """A simple box container for testing copy.replace().""" |
| 139 | + |
| 140 | + def __init__(self, inner_value: T) -> None: |
| 141 | + """Initialize with a value.""" |
| 142 | + super().__init__(inner_value) |
| 143 | + |
| 144 | + def __eq__(self, other: object) -> bool: |
| 145 | + """Check equality based on type and inner value.""" |
| 146 | + if not isinstance(other, CustomBox): |
| 147 | + return False |
| 148 | + return self._inner_value == other._inner_value |
| 149 | + |
| 150 | + def __hash__(self) -> int: |
| 151 | + """Hash based on the inner value.""" |
| 152 | + return hash(self._inner_value) |
| 153 | + |
| 154 | + def __repr__(self) -> str: |
| 155 | + """String representation.""" |
| 156 | + return f'CustomBox({self._inner_value!r})' |
| 157 | + |
| 158 | + # Test basic replacement works |
| 159 | + original = CustomBox('hello') |
| 160 | + replaced = copy.replace(original, _inner_value='world') |
| 161 | + |
| 162 | + assert replaced == CustomBox('world') |
| 163 | + assert replaced is not original |
| 164 | + assert replaced._inner_value == 'world' # noqa: SLF001 |
| 165 | + assert isinstance(replaced, CustomBox) # Preserves exact type |
| 166 | + |
| 167 | + # Test with no changes |
| 168 | + copied = copy.replace(original) |
| 169 | + assert copied == original |
| 170 | + # Unchanged objects return self per immutability principle |
| 171 | + assert copied is original |
| 172 | + |
| 173 | + # Test with invalid arguments |
| 174 | + with pytest.raises(TypeError): |
| 175 | + copy.replace(original, invalid_arg=True) |
0 commit comments