|
| 1 | +import copy |
| 2 | +import sys |
| 3 | +from typing import TYPE_CHECKING, Any |
| 4 | + |
| 5 | +import pytest |
| 6 | +from hypothesis import example, given |
| 7 | +from hypothesis import strategies as st |
| 8 | + |
| 9 | +from returns.primitives.container import BaseContainer |
| 10 | + |
| 11 | +# For Python < 3.13 compatibility: copy.replace doesn't exist in older Python |
| 12 | +if TYPE_CHECKING: # pragma: no cover |
| 13 | + # Defining a dummy replace function for type checking |
| 14 | + def _replace(container_instance: Any, /, inner_value: Any) -> Any: |
| 15 | + """Dummy replace function for type checking.""" |
| 16 | + return container_instance |
| 17 | + |
| 18 | + # Assigning it to copy.replace for type checking |
| 19 | + if not hasattr(copy, 'replace'): |
| 20 | + copy.replace = _replace # type: ignore |
| 21 | + |
| 22 | + |
| 23 | +class _CustomClass: |
| 24 | + """A custom class for replace testing.""" |
| 25 | + |
| 26 | + __slots__ = ('inner_value',) |
| 27 | + |
| 28 | + def __init__(self, inner_value: str) -> None: |
| 29 | + """Initialize instance.""" |
| 30 | + self.inner_value = inner_value |
| 31 | + |
| 32 | + def __eq__(self, other: object) -> bool: |
| 33 | + """Compare with other.""" |
| 34 | + if isinstance(other, _CustomClass): |
| 35 | + return self.inner_value == other.inner_value |
| 36 | + return NotImplemented |
| 37 | + |
| 38 | + def __ne__(self, other: object) -> bool: |
| 39 | + """Not equal to other.""" |
| 40 | + if isinstance(other, _CustomClass): |
| 41 | + return self.inner_value != other.inner_value |
| 42 | + return NotImplemented |
| 43 | + |
| 44 | + def __hash__(self) -> int: |
| 45 | + """Return hash of the inner value.""" |
| 46 | + return hash(self.inner_value) |
| 47 | + |
| 48 | + |
| 49 | +@given( |
| 50 | + st.one_of( |
| 51 | + st.integers(), |
| 52 | + st.floats(allow_nan=False), |
| 53 | + st.text(), |
| 54 | + st.booleans(), |
| 55 | + st.lists(st.text()), |
| 56 | + st.dictionaries(st.text(), st.integers()), |
| 57 | + st.builds(_CustomClass, st.text()), |
| 58 | + ), |
| 59 | +) |
| 60 | +@example(None) |
| 61 | +def test_replace_method(container_value: Any) -> None: |
| 62 | + """Ensures __replace__ magic method works as expected.""" |
| 63 | + container = BaseContainer(container_value) |
| 64 | + |
| 65 | + # Test with new inner_value returns a new container |
| 66 | + new_value = 'new_value' |
| 67 | + # Test direct call to __replace__ |
| 68 | + new_container = container.__replace__(new_value) # noqa: PLC2801 |
| 69 | + |
| 70 | + assert new_container is not container |
| 71 | + assert new_container._inner_value == new_value # noqa: SLF001 |
| 72 | + assert isinstance(new_container, BaseContainer) |
| 73 | + assert type(new_container) is type(container) # noqa: WPS516 |
| 74 | + |
| 75 | + |
| 76 | +def test_base_container_replace_direct_call(): |
| 77 | + """Test direct call to the __replace__ method.""" |
| 78 | + container = BaseContainer(1) # Create instance directly |
| 79 | + new_value = 'new_value' |
| 80 | + # Test direct call to __replace__ |
| 81 | + new_container = container.__replace__(new_value) # noqa: PLC2801 |
| 82 | + |
| 83 | + assert new_container is not container |
| 84 | + assert new_container._inner_value == new_value # noqa: SLF001 |
| 85 | + assert isinstance(new_container, BaseContainer) |
| 86 | + assert type(new_container) is type(container) # noqa: WPS516 |
| 87 | + |
| 88 | + |
| 89 | +def test_base_container_replace_direct_call_invalid_args(): |
| 90 | + """Test direct call with invalid arguments.""" |
| 91 | + container = BaseContainer(1) # Create instance directly |
| 92 | + # Direct call with no args should fail |
| 93 | + with pytest.raises(TypeError): |
| 94 | + container.__replace__() # noqa: PLC2801 |
| 95 | + |
| 96 | + # Direct call with keyword args matching the name is allowed by Python, |
| 97 | + # even with /. |
| 98 | + # If uncommented, it should pass as Python allows this. |
| 99 | + # Removing commented test case for |
| 100 | + # `container.__replace__(inner_value='new')` |
| 101 | + |
| 102 | + # Direct call with extra positional args should fail |
| 103 | + with pytest.raises(TypeError): |
| 104 | + container.__replace__('new', 'extra') # noqa: PLC2801 |
| 105 | + |
| 106 | + # Direct call with unexpected keyword args should fail |
| 107 | + with pytest.raises(TypeError): |
| 108 | + container.__replace__(other_kwarg='value') # type: ignore[attr-defined] |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.skipif( |
| 112 | + sys.version_info < (3, 13), |
| 113 | + reason='copy.replace requires Python 3.13+', |
| 114 | +) |
| 115 | +@given( |
| 116 | + st.one_of( |
| 117 | + st.integers(), |
| 118 | + st.floats(allow_nan=False), |
| 119 | + st.text(), |
| 120 | + st.booleans(), |
| 121 | + st.lists(st.text()), |
| 122 | + st.dictionaries(st.text(), st.integers()), |
| 123 | + st.builds(_CustomClass, st.text()), |
| 124 | + ), |
| 125 | +) |
| 126 | +@example(None) |
| 127 | +def test_copy_replace(container_value: Any) -> None: |
| 128 | + """Ensures copy.replace works with BaseContainer.""" |
| 129 | + container = BaseContainer(container_value) |
| 130 | + |
| 131 | + # Test with no changes is not directly possible via copy.replace with this |
| 132 | + # __replace__ implementation. |
| 133 | + # The copy.replace function itself handles the no-change case if the |
| 134 | + # object supports it, but our __replace__ requires a value. |
| 135 | + |
| 136 | + # Test with new inner_value returns a new container using copy.replace |
| 137 | + new_value = 'new_value' |
| 138 | + # copy.replace calls __replace__ with the new value as a positional arg |
| 139 | + new_container = copy.replace(container, new_value) # type: ignore[attr-defined] |
| 140 | + |
| 141 | + assert new_container is not container |
| 142 | + assert new_container._inner_value == new_value # noqa: SLF001 |
| 143 | + assert isinstance(new_container, BaseContainer) |
| 144 | + assert type(new_container) is type(container) # noqa: WPS516 |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.skipif( |
| 148 | + sys.version_info < (3, 13), |
| 149 | + reason='copy.replace requires Python 3.13+', |
| 150 | +) |
| 151 | +def test_base_container_replace_via_copy_no_changes(container_value): |
| 152 | + """Test copy.replace with no actual change in value.""" |
| 153 | + container = BaseContainer(container_value) |
| 154 | + |
| 155 | + # Test with no changes is not directly possible via copy.replace with this |
| 156 | + # __replace__ implementation. |
| 157 | + # The copy.replace function itself handles the no-change case if the |
| 158 | + # object supports it, but our __replace__ requires a value. |
| 159 | + # If copy.replace is called with the same value, it should work. |
| 160 | + new_container = copy.replace(container, inner_value=container_value) |
| 161 | + |
| 162 | + assert new_container is not container # A new instance should be created |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.skipif( |
| 166 | + sys.version_info < (3, 13), |
| 167 | + reason='copy.replace requires Python 3.13+', |
| 168 | +) |
| 169 | +def test_base_container_replace_via_copy_invalid_args(container): |
| 170 | + """Test copy.replace with invalid arguments.""" |
| 171 | + # copy.replace converts the keyword 'inner_value' to a positional arg |
| 172 | + # for __replace__(self, /, inner_value), so this is valid. |
| 173 | + # Removing commented out test case for copy.replace with inner_value kwarg |
| 174 | + |
| 175 | + # However, passing other keyword arguments will fail because __replace__ |
| 176 | + # doesn't accept them. |
| 177 | + with pytest.raises(TypeError): |
| 178 | + copy.replace(container, other_kwarg='value') # type: ignore[attr-defined] |
| 179 | + |
| 180 | + # copy.replace should raise TypeError if extra positional arguments |
| 181 | + # are passed. |
| 182 | + with pytest.raises(TypeError): |
| 183 | + copy.replace(container, 'new', 'extra') # type: ignore[attr-defined] |
| 184 | + |
| 185 | + # copy.replace should raise TypeError if no value is passed |
| 186 | + # (our __replace__ requires one). |
| 187 | + with pytest.raises(TypeError): |
| 188 | + copy.replace(container) # type: ignore[attr-defined] |
0 commit comments