10
10
11
11
# For Python < 3.13 compatibility: copy.replace doesn't exist in older Python
12
12
if TYPE_CHECKING : # pragma: no cover
13
-
13
+ # Defining a dummy replace function for type checking
14
14
def _replace (container_instance : Any , / , ** changes : Any ) -> Any :
15
15
"""Dummy replace function for type checking."""
16
16
return container_instance
17
17
18
+ # Assigning it to copy.replace for type checking
18
19
if not hasattr (copy , 'replace' ):
19
20
copy .replace = _replace # type: ignore
20
21
21
22
22
23
class _CustomClass :
24
+ """A custom class for replace testing."""
25
+
23
26
__slots__ = ('inner_value' ,)
24
27
25
28
def __init__ (self , inner_value : str ) -> None :
29
+ """Initialize instance."""
26
30
self .inner_value = inner_value
27
31
28
32
def __eq__ (self , other : object ) -> bool :
33
+ """Compare with other."""
29
34
if isinstance (other , _CustomClass ):
30
35
return self .inner_value == other .inner_value
31
36
return NotImplemented
32
37
33
38
def __ne__ (self , other : object ) -> bool :
39
+ """Not equal to other."""
34
40
if isinstance (other , _CustomClass ):
35
41
return self .inner_value != other .inner_value
36
42
return NotImplemented
37
43
38
44
def __hash__ (self ) -> int :
45
+ """Return hash of the inner value."""
39
46
return hash (self .inner_value )
40
47
41
48
@@ -52,9 +59,10 @@ def __hash__(self) -> int:
52
59
)
53
60
@example (None )
54
61
def test_replace (container_value : Any ) -> None :
55
- """Test __replace__ magic method."""
62
+ """Ensures __replace__ magic method works as expected ."""
56
63
container = BaseContainer (container_value )
57
64
65
+ # Test with new inner_value returns a new container
58
66
new_value = 'new_value'
59
67
new_container = container .__replace__ (_inner_value = new_value )
60
68
@@ -65,14 +73,15 @@ def test_replace(container_value: Any) -> None:
65
73
66
74
67
75
def test_replace_no_changes () -> None :
68
- """Test __replace__ with no changes."""
76
+ """Ensures __replace__ with no changes returns the same container ."""
69
77
container = BaseContainer ('test' )
78
+ # We need to call the method directly to test this functionality
70
79
result = container .__replace__ () # noqa: PLC2801
71
80
assert result is container
72
81
73
82
74
83
def test_replace_invalid_attributes () -> None :
75
- """Test __replace__ with invalid attributes."""
84
+ """Ensures __replace__ raises ValueError for invalid attributes."""
76
85
container = BaseContainer ('test' )
77
86
78
87
with pytest .raises (ValueError , match = 'Only _inner_value can be replaced' ):
@@ -99,11 +108,13 @@ def test_replace_invalid_attributes() -> None:
99
108
)
100
109
@example (None )
101
110
def test_copy_replace (container_value : Any ) -> None :
102
- """Test copy.replace with BaseContainer."""
111
+ """Ensures copy.replace works with BaseContainer."""
103
112
container = BaseContainer (container_value )
104
113
114
+ # Test with no changes returns the same container
105
115
assert copy .replace (container ) is container # type: ignore[attr-defined]
106
116
117
+ # Test with new inner_value returns a new container
107
118
new_value = 'new_value'
108
119
new_container = copy .replace (container , _inner_value = new_value ) # type: ignore[attr-defined]
109
120
@@ -118,7 +129,7 @@ def test_copy_replace(container_value: Any) -> None:
118
129
reason = 'copy.replace requires Python 3.13+' ,
119
130
)
120
131
def test_copy_replace_invalid_attributes () -> None :
121
- """Test copy.replace with invalid attributes."""
132
+ """Ensures copy.replace raises ValueError for invalid attributes."""
122
133
container = BaseContainer ('test' )
123
134
124
135
with pytest .raises (ValueError , match = 'Only _inner_value can be replaced' ):
0 commit comments