@@ -91,44 +91,49 @@ def mutable_during_init(
91
91
>>>
92
92
>>> @frozen_dataclass_sealable
93
93
... class Example:
94
- ... # Regular immutable field (required fields must come first)
95
94
... name: str
96
- ... # Using field with metadata directly (recommended approach)
97
95
... items: list[str] = field(
98
96
... default_factory=list,
99
97
... metadata={"mutable_during_init": True}
100
98
... )
101
- >>>
102
- >>> # Create an instance with deferred sealing
99
+
100
+ Create an instance with deferred sealing:
101
+
103
102
>>> example = Example(name="test-example")
104
- >>>
105
- >>> # Cannot modify immutable fields even before sealing
103
+
104
+ Cannot modify immutable fields even before sealing:
105
+
106
106
>>> try:
107
107
... example.name = "new-name"
108
108
... except AttributeError as e:
109
109
... print(f"Error: {type(e).__name__}")
110
110
Error: AttributeError
111
- >>>
112
- >>> # Can modify mutable field before sealing
111
+
112
+ Can modify mutable field before sealing:
113
+
113
114
>>> example.items.append("item1")
114
115
>>> example.items
115
116
['item1']
116
- >>>
117
- >>> # Now seal the object
117
+
118
+ Now seal the object:
119
+
118
120
>>> example.seal()
119
- >>>
120
- >>> # Verify the object is sealed
121
+
122
+ Verify the object is sealed:
123
+
121
124
>>> hasattr(example, "_sealed") and example._sealed
122
125
True
123
- >>>
124
- >>> # Cannot modify mutable field after sealing
126
+
127
+ Cannot modify mutable field after sealing:
128
+
125
129
>>> try:
126
130
... example.items = ["new-item"]
127
131
... except AttributeError as e:
128
132
... print(f"Error: {type(e).__name__}")
129
133
Error: AttributeError
130
- >>>
131
- >>> # But can still modify the contents of mutable containers
134
+
135
+ But can still modify the contents of mutable containers:
136
+
132
137
>>> example.items.append("item2")
133
138
>>> example.items
134
139
['item1', 'item2']
@@ -239,7 +244,7 @@ def frozen_dataclass_sealable(
239
244
Examples
240
245
--------
241
246
Basic usage:
242
-
247
+
243
248
>>> from dataclasses import field
244
249
>>> from typing import Optional
245
250
>>> from libtmux._internal.frozen_dataclass_sealable import (
@@ -253,85 +258,99 @@ def frozen_dataclass_sealable(
253
258
... default_factory=dict,
254
259
... metadata={"mutable_during_init": True}
255
260
... )
256
- >>>
257
- >>> # Create an instance
261
+
262
+ Create an instance:
263
+
258
264
>>> config = Config(name="test-config")
259
265
>>> config.name
260
266
'test-config'
261
- >>>
262
- >>> # Cannot modify frozen field
267
+
268
+ Cannot modify frozen field:
269
+
263
270
>>> try:
264
271
... config.name = "modified"
265
272
... except AttributeError as e:
266
273
... print(f"Error: {type(e).__name__}")
267
274
Error: AttributeError
268
- >>>
269
- >>> # Can modify mutable field before sealing
275
+
276
+ Can modify mutable field before sealing:
277
+
270
278
>>> config.values["key1"] = 100
271
279
>>> config.values
272
280
{'key1': 100}
273
- >>>
274
- >>> # Can also directly assign to mutable field before sealing
281
+
282
+ Can also directly assign to mutable field before sealing:
283
+
275
284
>>> new_values = {"key2": 200}
276
285
>>> config.values = new_values
277
286
>>> config.values
278
287
{'key2': 200}
279
- >>>
280
- >>> # Seal the object
288
+
289
+ Seal the object:
290
+
281
291
>>> config.seal()
282
- >>>
283
- >>> # Verify the object is sealed
292
+
293
+ Verify the object is sealed:
294
+
284
295
>>> hasattr(config, "_sealed") and config._sealed
285
296
True
286
- >>>
287
- >>> # Cannot modify mutable field after sealing
297
+
298
+ Cannot modify mutable field after sealing:
299
+
288
300
>>> try:
289
301
... config.values = {"key3": 300}
290
302
... except AttributeError as e:
291
303
... print(f"Error: {type(e).__name__}")
292
304
Error: AttributeError
293
- >>>
294
- >>> # But can still modify the contents of mutable containers after sealing
305
+
306
+ But can still modify the contents of mutable containers after sealing:
307
+
295
308
>>> config.values["key3"] = 300
296
309
>>> config.values
297
310
{'key2': 200, 'key3': 300}
298
311
299
312
With deferred sealing:
300
-
313
+
301
314
>>> @frozen_dataclass_sealable
302
315
... class Node:
303
316
... value: int
304
317
... next_node: Optional['Node'] = field(
305
318
... default=None,
306
319
... metadata={"mutable_during_init": True}
307
320
... )
308
- >>>
309
- >>> # Create a linked list
321
+
322
+ Create a linked list:
323
+
310
324
>>> node1 = Node(value=1) # Not sealed automatically
311
325
>>> node2 = Node(value=2) # Not sealed automatically
312
- >>>
313
- >>> # Can modify mutable field before sealing
326
+
327
+ Can modify mutable field before sealing:
328
+
314
329
>>> node1.next_node = node2
315
- >>>
316
- >>> # Verify structure
330
+
331
+ Verify structure:
332
+
317
333
>>> node1.value
318
334
1
319
335
>>> node2.value
320
336
2
321
337
>>> node1.next_node is node2
322
338
True
323
- >>>
324
- >>> # Seal nodes
339
+
340
+ Seal nodes:
341
+
325
342
>>> node1.seal()
326
343
>>> node2.seal()
327
- >>>
328
- >>> # Verify sealed status
344
+
345
+ Verify sealed status:
346
+
329
347
>>> hasattr(node1, "_sealed") and node1._sealed
330
348
True
331
349
>>> hasattr(node2, "_sealed") and node2._sealed
332
350
True
333
- >>>
334
- >>> # Cannot modify mutable field after sealing
351
+
352
+ Cannot modify mutable field after sealing:
353
+
335
354
>>> try:
336
355
... node1.next_node = None
337
356
... except AttributeError as e:
0 commit comments