77
881. Field-level mutability control:
99
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
1111 during the initialization phase but become immutable after sealing.
1212
13132. Two-phase initialization:
@@ -50,11 +50,14 @@ def mutable_field(
5050) -> dataclasses .Field [t .Any ]:
5151 """Create a field that is mutable during initialization but immutable after sealing.
5252
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
5557
5658 Returns
5759 -------
60+ dataclasses.Field
5861 A dataclass Field with metadata indicating it's mutable during initialization
5962 """
6063 return dataclasses .field (
@@ -69,15 +72,20 @@ def mutable_during_init(
6972
7073 This decorator applies to a method that returns the field's default value.
7174
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
7479
7580 Returns
7681 -------
82+ dataclasses.Field
7783 A dataclass Field with metadata indicating it's mutable during initialization
7884
79- Example:
80- ```python
85+ Examples
86+ --------
87+ .. code-block:: python
88+
8189 @frozen_dataclass_sealable
8290 class Example:
8391 # Using field with metadata directly (recommended)
@@ -93,7 +101,6 @@ def values(self) -> dict[str, int]:
93101
94102 # Regular immutable field
95103 name: str
96- ```
97104 """
98105 if field_method is None :
99106 # Used with parentheses: @mutable_during_init()
@@ -129,37 +136,41 @@ def seal(self) -> None:
129136def is_sealable (cls_or_obj : t .Any ) -> bool :
130137 """Check if a class or object is sealable.
131138
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
134143
135144 Returns
136145 -------
146+ bool
137147 True if the class or object is sealable, False otherwise
138148
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
163174 """
164175 # If it's a class, check if it has a seal method
165176 if isinstance (cls_or_obj , type ):
@@ -176,23 +187,30 @@ def frozen_dataclass_sealable(
176187 """Create a dataclass that is immutable, with field-level mutability control.
177188
178189 Enhances the standard dataclass with:
190+
179191 - Core immutability (like dataclasses.frozen=True)
180192 - Field-level mutability control during initialization
181193 - Explicit sealing mechanism
182194 - Support for inheritance from mutable base classes
183195
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
187202
188203 Returns
189204 -------
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
191207
192208 Examples
193209 --------
194- Basic usage:
195- ```python
210+ Basic usage:
211+
212+ .. code-block:: python
213+
196214 @frozen_dataclass_sealable
197215 class Config:
198216 name: str
@@ -201,10 +219,11 @@ class Config:
201219 default_factory=dict,
202220 metadata={"mutable_during_init": True}
203221 )
204- ```
205222
206- With deferred sealing:
207- ```python
223+ With deferred sealing:
224+
225+ .. code-block:: python
226+
208227 @frozen_dataclass_sealable
209228 class Node:
210229 value: int
@@ -222,7 +241,6 @@ class Node:
222241 # Seal nodes
223242 node1.seal()
224243 node2.seal()
225- ```
226244 """
227245 # Support both @frozen_dataclass_sealable and @frozen_dataclass_sealable() usage
228246 if cls is None :
@@ -428,8 +446,10 @@ def custom_init(self: t.Any, *args: t.Any, **kwargs: t.Any) -> None:
428446 def seal (self : t .Any , deep : bool = False ) -> None :
429447 """Seal the object to prevent further modifications.
430448
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
433453 """
434454 # First seal this object
435455 object .__setattr__ (self , "_sealed" , True )
@@ -453,7 +473,13 @@ def seal(self: t.Any, deep: bool = False) -> None:
453473 # Add a class method to check if the class is sealable
454474 @classmethod
455475 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+ """
457483 return True
458484
459485 cls .is_sealable = is_sealable # type: ignore
0 commit comments