c108.dataclasses
Dataclasses tools.
Provides a decorator that adds a merge() method to dataclasses, enabling functional-style updates with sentinel values to distinguish "not provided" from None or other values.
Mergeable
Bases: Protocol[T]
Protocol for classes decorated with @mergeable.
Source code in c108/dataclasses.py
29 30 31 32 33 34 35 | |
merge(**kwargs)
Create a new instance with selectively updated fields.
Source code in c108/dataclasses.py
33 34 35 | |
mergeable(cls=None, *, sentinel=UNSET, include=None, exclude=None, include_private=True)
Decorator that adds a merge() method to a dataclass for creating modified copies.
The merge() method creates a new instance with selectively updated fields, using a sentinel value to distinguish "not provided" from None or other values.
Similar to dataclasses.replace() but with sentinel support and chainable syntax.
Can be used with or without parentheses
@mergeable # Uses all defaults @mergeable() # Same as above @mergeable(sentinel=None) # With parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T] | None
|
The dataclass to decorate (automatically provided when used without parentheses) |
None
|
sentinel
|
Sentinel value indicating "use existing value" (default: UNSET). Common values: UNSET, MISSING (for distinguishing from None) or None. |
UNSET
|
|
include
|
list[str] | None
|
If provided, ONLY these fields can be merged (whitelist mode). Overrides default field discovery. Can explicitly include private fields. Cannot be used together with exclude. |
None
|
exclude
|
list[str] | None
|
If provided, these fields are excluded from merging (blacklist mode). Applied after default field discovery. Cannot be used together with include. |
None
|
include_private
|
bool
|
If True (default), private fields (starting with '_') can be merged, matching dataclasses.replace() behavior. If False, private fields are excluded by default. |
True
|
Returns:
| Type | Description |
|---|---|
type[Mergeable[T]] | Callable[[type[T]], type[Mergeable[T]]]
|
Decorated class with merge() method added, or decorator function if called with arguments. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cls is not a dataclass |
ValueError
|
If both include and exclude are specified |
ValueError
|
If include/exclude reference non-existent fields |
ValueError
|
If include references fields with init=False |
ValueError
|
If include references InitVar fields |
Examples:
>>> @mergeable
... @dataclass
... class Config:
... timeout: int = 30
... retries: int = 3
... def merge(self, **kwargs) -> Self:
... '''New Config instance with selectively updated fields'''
... # This is a stub for Docs and type hinting
... raise NotImplementedError("Implementation handled by @mergeable")
>>> c1 = Config()
>>> c2 = c1.merge(timeout=60)
>>> c2.timeout
60
>>> @mergeable(sentinel=None)
... @dataclass
... class Options:
... value: int | None = 5
... def merge(self, **kwargs) -> Self:
... '''New Options instance with selectively updated fields'''
>>> o1 = Options()
>>> o2 = o1.merge(value=None) # None means "keep existing"
>>> o2.value
5
>>> @mergeable(include=['timeout'])
... @dataclass
... class Limited:
... timeout: int = 30
... internal: int = 99
... def merge(self, **kwargs) -> Self:
... '''New Limited instance with selectively updated fields'''
>>> lim = Limited()
>>> lim.merge(timeout=60)
Limited(timeout=60, internal=99)
Notes
- Only fields with init=True are mergeable
- InitVar fields are never mergeable
- Fields with init=False are reset to defaults in new instance
- Uses shallow copy semantics for field values
Source code in c108/dataclasses.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |