Skip to content

Commit 1542ff6

Browse files
committed
style(doctest): improve doctest readability with narrative descriptions
1 parent c0ee523 commit 1542ff6

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed

src/libtmux/_internal/frozen_dataclass_sealable.py

+65-46
Original file line numberDiff line numberDiff line change
@@ -91,44 +91,49 @@ def mutable_during_init(
9191
>>>
9292
>>> @frozen_dataclass_sealable
9393
... class Example:
94-
... # Regular immutable field (required fields must come first)
9594
... name: str
96-
... # Using field with metadata directly (recommended approach)
9795
... items: list[str] = field(
9896
... default_factory=list,
9997
... metadata={"mutable_during_init": True}
10098
... )
101-
>>>
102-
>>> # Create an instance with deferred sealing
99+
100+
Create an instance with deferred sealing:
101+
103102
>>> example = Example(name="test-example")
104-
>>>
105-
>>> # Cannot modify immutable fields even before sealing
103+
104+
Cannot modify immutable fields even before sealing:
105+
106106
>>> try:
107107
... example.name = "new-name"
108108
... except AttributeError as e:
109109
... print(f"Error: {type(e).__name__}")
110110
Error: AttributeError
111-
>>>
112-
>>> # Can modify mutable field before sealing
111+
112+
Can modify mutable field before sealing:
113+
113114
>>> example.items.append("item1")
114115
>>> example.items
115116
['item1']
116-
>>>
117-
>>> # Now seal the object
117+
118+
Now seal the object:
119+
118120
>>> example.seal()
119-
>>>
120-
>>> # Verify the object is sealed
121+
122+
Verify the object is sealed:
123+
121124
>>> hasattr(example, "_sealed") and example._sealed
122125
True
123-
>>>
124-
>>> # Cannot modify mutable field after sealing
126+
127+
Cannot modify mutable field after sealing:
128+
125129
>>> try:
126130
... example.items = ["new-item"]
127131
... except AttributeError as e:
128132
... print(f"Error: {type(e).__name__}")
129133
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+
132137
>>> example.items.append("item2")
133138
>>> example.items
134139
['item1', 'item2']
@@ -239,7 +244,7 @@ def frozen_dataclass_sealable(
239244
Examples
240245
--------
241246
Basic usage:
242-
247+
243248
>>> from dataclasses import field
244249
>>> from typing import Optional
245250
>>> from libtmux._internal.frozen_dataclass_sealable import (
@@ -253,85 +258,99 @@ def frozen_dataclass_sealable(
253258
... default_factory=dict,
254259
... metadata={"mutable_during_init": True}
255260
... )
256-
>>>
257-
>>> # Create an instance
261+
262+
Create an instance:
263+
258264
>>> config = Config(name="test-config")
259265
>>> config.name
260266
'test-config'
261-
>>>
262-
>>> # Cannot modify frozen field
267+
268+
Cannot modify frozen field:
269+
263270
>>> try:
264271
... config.name = "modified"
265272
... except AttributeError as e:
266273
... print(f"Error: {type(e).__name__}")
267274
Error: AttributeError
268-
>>>
269-
>>> # Can modify mutable field before sealing
275+
276+
Can modify mutable field before sealing:
277+
270278
>>> config.values["key1"] = 100
271279
>>> config.values
272280
{'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+
275284
>>> new_values = {"key2": 200}
276285
>>> config.values = new_values
277286
>>> config.values
278287
{'key2': 200}
279-
>>>
280-
>>> # Seal the object
288+
289+
Seal the object:
290+
281291
>>> config.seal()
282-
>>>
283-
>>> # Verify the object is sealed
292+
293+
Verify the object is sealed:
294+
284295
>>> hasattr(config, "_sealed") and config._sealed
285296
True
286-
>>>
287-
>>> # Cannot modify mutable field after sealing
297+
298+
Cannot modify mutable field after sealing:
299+
288300
>>> try:
289301
... config.values = {"key3": 300}
290302
... except AttributeError as e:
291303
... print(f"Error: {type(e).__name__}")
292304
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+
295308
>>> config.values["key3"] = 300
296309
>>> config.values
297310
{'key2': 200, 'key3': 300}
298311
299312
With deferred sealing:
300-
313+
301314
>>> @frozen_dataclass_sealable
302315
... class Node:
303316
... value: int
304317
... next_node: Optional['Node'] = field(
305318
... default=None,
306319
... metadata={"mutable_during_init": True}
307320
... )
308-
>>>
309-
>>> # Create a linked list
321+
322+
Create a linked list:
323+
310324
>>> node1 = Node(value=1) # Not sealed automatically
311325
>>> node2 = Node(value=2) # Not sealed automatically
312-
>>>
313-
>>> # Can modify mutable field before sealing
326+
327+
Can modify mutable field before sealing:
328+
314329
>>> node1.next_node = node2
315-
>>>
316-
>>> # Verify structure
330+
331+
Verify structure:
332+
317333
>>> node1.value
318334
1
319335
>>> node2.value
320336
2
321337
>>> node1.next_node is node2
322338
True
323-
>>>
324-
>>> # Seal nodes
339+
340+
Seal nodes:
341+
325342
>>> node1.seal()
326343
>>> node2.seal()
327-
>>>
328-
>>> # Verify sealed status
344+
345+
Verify sealed status:
346+
329347
>>> hasattr(node1, "_sealed") and node1._sealed
330348
True
331349
>>> hasattr(node2, "_sealed") and node2._sealed
332350
True
333-
>>>
334-
>>> # Cannot modify mutable field after sealing
351+
352+
Cannot modify mutable field after sealing:
353+
335354
>>> try:
336355
... node1.next_node = None
337356
... except AttributeError as e:

0 commit comments

Comments
 (0)