Skip to content

Commit c0ee523

Browse files
committed
docs(frozen_dataclass): enhance doctests to demonstrate behavior before and after sealing
1 parent fc4ac66 commit c0ee523

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

src/libtmux/_internal/frozen_dataclass_sealable.py

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,39 @@ def mutable_during_init(
9999
... metadata={"mutable_during_init": True}
100100
... )
101101
>>>
102-
>>> # Create an instance
102+
>>> # Create an instance with deferred sealing
103103
>>> example = Example(name="test-example")
104104
>>>
105-
>>> # Can modify mutable field
105+
>>> # Cannot modify immutable fields even before sealing
106+
>>> try:
107+
... example.name = "new-name"
108+
... except AttributeError as e:
109+
... print(f"Error: {type(e).__name__}")
110+
Error: AttributeError
111+
>>>
112+
>>> # Can modify mutable field before sealing
106113
>>> example.items.append("item1")
107114
>>> example.items
108115
['item1']
116+
>>>
117+
>>> # Now seal the object
118+
>>> example.seal()
119+
>>>
120+
>>> # Verify the object is sealed
121+
>>> hasattr(example, "_sealed") and example._sealed
122+
True
123+
>>>
124+
>>> # Cannot modify mutable field after sealing
125+
>>> try:
126+
... example.items = ["new-item"]
127+
... except AttributeError as e:
128+
... print(f"Error: {type(e).__name__}")
129+
Error: AttributeError
130+
>>>
131+
>>> # But can still modify the contents of mutable containers
132+
>>> example.items.append("item2")
133+
>>> example.items
134+
['item1', 'item2']
109135
"""
110136
if field_method is None:
111137
# Used with parentheses: @mutable_during_init()
@@ -236,22 +262,39 @@ def frozen_dataclass_sealable(
236262
>>> # Cannot modify frozen field
237263
>>> try:
238264
... config.name = "modified"
239-
... except AttributeError:
240-
... print("Cannot modify frozen field")
241-
Cannot modify frozen field
265+
... except AttributeError as e:
266+
... print(f"Error: {type(e).__name__}")
267+
Error: AttributeError
242268
>>>
243-
>>> # Can modify mutable field
269+
>>> # Can modify mutable field before sealing
244270
>>> config.values["key1"] = 100
245271
>>> config.values
246272
{'key1': 100}
247273
>>>
274+
>>> # Can also directly assign to mutable field before sealing
275+
>>> new_values = {"key2": 200}
276+
>>> config.values = new_values
277+
>>> config.values
278+
{'key2': 200}
279+
>>>
248280
>>> # Seal the object
249281
>>> config.seal()
250282
>>>
251-
>>> # Can still modify the contents of mutable containers after sealing
252-
>>> config.values["key2"] = 200
283+
>>> # Verify the object is sealed
284+
>>> hasattr(config, "_sealed") and config._sealed
285+
True
286+
>>>
287+
>>> # Cannot modify mutable field after sealing
288+
>>> try:
289+
... config.values = {"key3": 300}
290+
... except AttributeError as e:
291+
... print(f"Error: {type(e).__name__}")
292+
Error: AttributeError
293+
>>>
294+
>>> # But can still modify the contents of mutable containers after sealing
295+
>>> config.values["key3"] = 300
253296
>>> config.values
254-
{'key1': 100, 'key2': 200}
297+
{'key2': 200, 'key3': 300}
255298
256299
With deferred sealing:
257300
@@ -266,6 +309,8 @@ def frozen_dataclass_sealable(
266309
>>> # Create a linked list
267310
>>> node1 = Node(value=1) # Not sealed automatically
268311
>>> node2 = Node(value=2) # Not sealed automatically
312+
>>>
313+
>>> # Can modify mutable field before sealing
269314
>>> node1.next_node = node2
270315
>>>
271316
>>> # Verify structure
@@ -285,6 +330,13 @@ def frozen_dataclass_sealable(
285330
True
286331
>>> hasattr(node2, "_sealed") and node2._sealed
287332
True
333+
>>>
334+
>>> # Cannot modify mutable field after sealing
335+
>>> try:
336+
... node1.next_node = None
337+
... except AttributeError as e:
338+
... print(f"Error: {type(e).__name__}")
339+
Error: AttributeError
288340
"""
289341
# Support both @frozen_dataclass_sealable and @frozen_dataclass_sealable() usage
290342
if cls is None:

0 commit comments

Comments
 (0)