@@ -84,23 +84,28 @@ def mutable_during_init(
84
84
85
85
Examples
86
86
--------
87
- .. code-block:: python
88
-
89
- @frozen_dataclass_sealable
90
- class Example:
91
- # Using field with metadata directly (recommended)
92
- items: list[str] = field(
93
- default_factory=list,
94
- metadata={"mutable_during_init": True}
95
- )
96
-
97
- # Alternative approach using the decorator
98
- @mutable_during_init
99
- def values(self) -> dict[str, int]:
100
- return {}
101
-
102
- # Regular immutable field
103
- name: str
87
+ >>> from dataclasses import field
88
+ >>> from libtmux._internal.frozen_dataclass_sealable import (
89
+ ... frozen_dataclass_sealable, mutable_during_init
90
+ ... )
91
+ >>>
92
+ >>> @frozen_dataclass_sealable
93
+ ... class Example:
94
+ ... # Regular immutable field (required fields must come first)
95
+ ... name: str
96
+ ... # Using field with metadata directly (recommended approach)
97
+ ... items: list[str] = field(
98
+ ... default_factory=list,
99
+ ... metadata={"mutable_during_init": True}
100
+ ... )
101
+ >>>
102
+ >>> # Create an instance
103
+ >>> example = Example(name="test-example")
104
+ >>>
105
+ >>> # Can modify mutable field
106
+ >>> example.items.append("item1")
107
+ >>> example.items
108
+ ['item1']
104
109
"""
105
110
if field_method is None :
106
111
# Used with parentheses: @mutable_during_init()
@@ -209,38 +214,77 @@ def frozen_dataclass_sealable(
209
214
--------
210
215
Basic usage:
211
216
212
- .. code-block:: python
213
-
214
- @frozen_dataclass_sealable
215
- class Config:
216
- name: str
217
-
218
- values: dict[str, int] = field(
219
- default_factory=dict,
220
- metadata={"mutable_during_init": True}
221
- )
217
+ >>> from dataclasses import field
218
+ >>> from typing import Optional
219
+ >>> from libtmux._internal.frozen_dataclass_sealable import (
220
+ ... frozen_dataclass_sealable, is_sealable
221
+ ... )
222
+ >>>
223
+ >>> @frozen_dataclass_sealable
224
+ ... class Config:
225
+ ... name: str
226
+ ... values: dict[str, int] = field(
227
+ ... default_factory=dict,
228
+ ... metadata={"mutable_during_init": True}
229
+ ... )
230
+ >>>
231
+ >>> # Create an instance
232
+ >>> config = Config(name="test-config")
233
+ >>> config.name
234
+ 'test-config'
235
+ >>>
236
+ >>> # Cannot modify frozen field
237
+ >>> try:
238
+ ... config.name = "modified"
239
+ ... except AttributeError:
240
+ ... print("Cannot modify frozen field")
241
+ Cannot modify frozen field
242
+ >>>
243
+ >>> # Can modify mutable field
244
+ >>> config.values["key1"] = 100
245
+ >>> config.values
246
+ {'key1': 100}
247
+ >>>
248
+ >>> # Seal the object
249
+ >>> config.seal()
250
+ >>>
251
+ >>> # Can still modify the contents of mutable containers after sealing
252
+ >>> config.values["key2"] = 200
253
+ >>> config.values
254
+ {'key1': 100, 'key2': 200}
222
255
223
256
With deferred sealing:
224
257
225
- .. code-block:: python
226
-
227
- @frozen_dataclass_sealable
228
- class Node:
229
- value: int
230
-
231
- next_node: Optional['Node'] = field(
232
- default=None,
233
- metadata={"mutable_during_init": True}
234
- )
235
-
236
- # Create a linked list
237
- node1 = Node(value=1) # Not sealed automatically
238
- node2 = Node(value=2) # Not sealed automatically
239
- node1.next_node = node2
240
-
241
- # Seal nodes
242
- node1.seal()
243
- node2.seal()
258
+ >>> @frozen_dataclass_sealable
259
+ ... class Node:
260
+ ... value: int
261
+ ... next_node: Optional['Node'] = field(
262
+ ... default=None,
263
+ ... metadata={"mutable_during_init": True}
264
+ ... )
265
+ >>>
266
+ >>> # Create a linked list
267
+ >>> node1 = Node(value=1) # Not sealed automatically
268
+ >>> node2 = Node(value=2) # Not sealed automatically
269
+ >>> node1.next_node = node2
270
+ >>>
271
+ >>> # Verify structure
272
+ >>> node1.value
273
+ 1
274
+ >>> node2.value
275
+ 2
276
+ >>> node1.next_node is node2
277
+ True
278
+ >>>
279
+ >>> # Seal nodes
280
+ >>> node1.seal()
281
+ >>> node2.seal()
282
+ >>>
283
+ >>> # Verify sealed status
284
+ >>> hasattr(node1, "_sealed") and node1._sealed
285
+ True
286
+ >>> hasattr(node2, "_sealed") and node2._sealed
287
+ True
244
288
"""
245
289
# Support both @frozen_dataclass_sealable and @frozen_dataclass_sealable() usage
246
290
if cls is None :
0 commit comments