7
7
8
8
1. Field-level mutability control:
9
9
10
- Use the `mutable_during_init` decorator to mark fields that should be mutable
10
+ Use the `` mutable_during_init` ` decorator to mark fields that should be mutable
11
11
during the initialization phase but become immutable after sealing.
12
12
13
13
2. Two-phase initialization:
@@ -50,11 +50,14 @@ def mutable_field(
50
50
) -> dataclasses .Field [t .Any ]:
51
51
"""Create a field that is mutable during initialization but immutable after sealing.
52
52
53
- Args:
54
- factory: A callable that returns the default value for the field
53
+ Parameters
54
+ ----------
55
+ factory : callable, optional
56
+ A callable that returns the default value for the field, by default list
55
57
56
58
Returns
57
59
-------
60
+ dataclasses.Field
58
61
A dataclass Field with metadata indicating it's mutable during initialization
59
62
"""
60
63
return dataclasses .field (
@@ -69,15 +72,20 @@ def mutable_during_init(
69
72
70
73
This decorator applies to a method that returns the field's default value.
71
74
72
- Args:
73
- field_method: A method that returns the default value for the field
75
+ Parameters
76
+ ----------
77
+ field_method : callable, optional
78
+ A method that returns the default value for the field, by default None
74
79
75
80
Returns
76
81
-------
82
+ dataclasses.Field
77
83
A dataclass Field with metadata indicating it's mutable during initialization
78
84
79
- Example:
80
- ```python
85
+ Examples
86
+ --------
87
+ .. code-block:: python
88
+
81
89
@frozen_dataclass_sealable
82
90
class Example:
83
91
# Using field with metadata directly (recommended)
@@ -93,7 +101,6 @@ def values(self) -> dict[str, int]:
93
101
94
102
# Regular immutable field
95
103
name: str
96
- ```
97
104
"""
98
105
if field_method is None :
99
106
# Used with parentheses: @mutable_during_init()
@@ -129,37 +136,41 @@ def seal(self) -> None:
129
136
def is_sealable (cls_or_obj : t .Any ) -> bool :
130
137
"""Check if a class or object is sealable.
131
138
132
- Args:
133
- cls_or_obj: The class or object to check
139
+ Parameters
140
+ ----------
141
+ cls_or_obj : Any
142
+ The class or object to check
134
143
135
144
Returns
136
145
-------
146
+ bool
137
147
True if the class or object is sealable, False otherwise
138
148
139
- Examples:
140
- >>> from dataclasses import dataclass
141
- >>> from libtmux._internal.frozen_dataclass_sealable import (
142
- ... frozen_dataclass_sealable, is_sealable
143
- ... )
144
-
145
- >>> # Regular class is not sealable
146
- >>> @dataclass
147
- ... class Regular:
148
- ... value: int
149
-
150
- >>> is_sealable(Regular)
151
- False
152
- >>> regular = Regular(value=42)
153
- >>> is_sealable(regular)
154
- False
155
-
156
- >>> # Non-class objects are not sealable
157
- >>> is_sealable("string")
158
- False
159
- >>> is_sealable(42)
160
- False
161
- >>> is_sealable(None)
162
- False
149
+ Examples
150
+ --------
151
+ >>> from dataclasses import dataclass
152
+ >>> from libtmux._internal.frozen_dataclass_sealable import (
153
+ ... frozen_dataclass_sealable, is_sealable
154
+ ... )
155
+
156
+ >>> # Regular class is not sealable
157
+ >>> @dataclass
158
+ ... class Regular:
159
+ ... value: int
160
+
161
+ >>> is_sealable(Regular)
162
+ False
163
+ >>> regular = Regular(value=42)
164
+ >>> is_sealable(regular)
165
+ False
166
+
167
+ >>> # Non-class objects are not sealable
168
+ >>> is_sealable("string")
169
+ False
170
+ >>> is_sealable(42)
171
+ False
172
+ >>> is_sealable(None)
173
+ False
163
174
"""
164
175
# If it's a class, check if it has a seal method
165
176
if isinstance (cls_or_obj , type ):
@@ -176,23 +187,30 @@ def frozen_dataclass_sealable(
176
187
"""Create a dataclass that is immutable, with field-level mutability control.
177
188
178
189
Enhances the standard dataclass with:
190
+
179
191
- Core immutability (like dataclasses.frozen=True)
180
192
- Field-level mutability control during initialization
181
193
- Explicit sealing mechanism
182
194
- Support for inheritance from mutable base classes
183
195
184
- Args:
185
- cls: The class to decorate
186
- **kwargs: Additional arguments passed to dataclasses.dataclass
196
+ Parameters
197
+ ----------
198
+ cls : type, optional
199
+ The class to decorate, by default None
200
+ **kwargs : dict
201
+ Additional arguments passed to dataclasses.dataclass
187
202
188
203
Returns
189
204
-------
190
- A decorated class with immutability features
205
+ type or callable
206
+ A decorated class with immutability features, or a decorator function if cls is None
191
207
192
208
Examples
193
209
--------
194
- Basic usage:
195
- ```python
210
+ Basic usage:
211
+
212
+ .. code-block:: python
213
+
196
214
@frozen_dataclass_sealable
197
215
class Config:
198
216
name: str
@@ -201,10 +219,11 @@ class Config:
201
219
default_factory=dict,
202
220
metadata={"mutable_during_init": True}
203
221
)
204
- ```
205
222
206
- With deferred sealing:
207
- ```python
223
+ With deferred sealing:
224
+
225
+ .. code-block:: python
226
+
208
227
@frozen_dataclass_sealable
209
228
class Node:
210
229
value: int
@@ -222,7 +241,6 @@ class Node:
222
241
# Seal nodes
223
242
node1.seal()
224
243
node2.seal()
225
- ```
226
244
"""
227
245
# Support both @frozen_dataclass_sealable and @frozen_dataclass_sealable() usage
228
246
if cls is None :
@@ -428,8 +446,10 @@ def custom_init(self: t.Any, *args: t.Any, **kwargs: t.Any) -> None:
428
446
def seal (self : t .Any , deep : bool = False ) -> None :
429
447
"""Seal the object to prevent further modifications.
430
448
431
- Args:
432
- deep: If True, recursively seal any nested sealable objects
449
+ Parameters
450
+ ----------
451
+ deep : bool, optional
452
+ If True, recursively seal any nested sealable objects, by default False
433
453
"""
434
454
# First seal this object
435
455
object .__setattr__ (self , "_sealed" , True )
@@ -453,7 +473,13 @@ def seal(self: t.Any, deep: bool = False) -> None:
453
473
# Add a class method to check if the class is sealable
454
474
@classmethod
455
475
def is_sealable (cls ) -> bool :
456
- """Check if this class is sealable."""
476
+ """Check if this class is sealable.
477
+
478
+ Returns
479
+ -------
480
+ bool
481
+ Always returns True for classes decorated with frozen_dataclass_sealable
482
+ """
457
483
return True
458
484
459
485
cls .is_sealable = is_sealable # type: ignore
0 commit comments