@@ -99,13 +99,39 @@ def mutable_during_init(
99
99
... metadata={"mutable_during_init": True}
100
100
... )
101
101
>>>
102
- >>> # Create an instance
102
+ >>> # Create an instance with deferred sealing
103
103
>>> example = Example(name="test-example")
104
104
>>>
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
106
113
>>> example.items.append("item1")
107
114
>>> example.items
108
115
['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']
109
135
"""
110
136
if field_method is None :
111
137
# Used with parentheses: @mutable_during_init()
@@ -236,22 +262,39 @@ def frozen_dataclass_sealable(
236
262
>>> # Cannot modify frozen field
237
263
>>> try:
238
264
... 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
242
268
>>>
243
- >>> # Can modify mutable field
269
+ >>> # Can modify mutable field before sealing
244
270
>>> config.values["key1"] = 100
245
271
>>> config.values
246
272
{'key1': 100}
247
273
>>>
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
+ >>>
248
280
>>> # Seal the object
249
281
>>> config.seal()
250
282
>>>
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
253
296
>>> config.values
254
- {'key1 ': 100 , 'key2 ': 200 }
297
+ {'key2 ': 200 , 'key3 ': 300 }
255
298
256
299
With deferred sealing:
257
300
@@ -266,6 +309,8 @@ def frozen_dataclass_sealable(
266
309
>>> # Create a linked list
267
310
>>> node1 = Node(value=1) # Not sealed automatically
268
311
>>> node2 = Node(value=2) # Not sealed automatically
312
+ >>>
313
+ >>> # Can modify mutable field before sealing
269
314
>>> node1.next_node = node2
270
315
>>>
271
316
>>> # Verify structure
@@ -285,6 +330,13 @@ def frozen_dataclass_sealable(
285
330
True
286
331
>>> hasattr(node2, "_sealed") and node2._sealed
287
332
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
288
340
"""
289
341
# Support both @frozen_dataclass_sealable and @frozen_dataclass_sealable() usage
290
342
if cls is None :
0 commit comments