mixin
DataclassAdaptable
Mixin class providing the ability to convert (adapt) one dataclass type to another.
Source code in fancy_dataclass/mixin.py
adapt_to(dest_type)
Converts a DataclassAdaptable
object to another type, dest_type
.
By default this will attempt to coerce the fields from the original type to the new type, but subclasses may override the behavior, e.g. to allow field renaming.
Source code in fancy_dataclass/mixin.py
coerce(obj)
classmethod
Constructs a DataclassAdaptable
object from the attributes of an arbitrary object.
Any missing attributes will be set to their default values.
DataclassMixin
Mixin class for adding some kind functionality to a dataclass.
For example, this could provide features for conversion to/from JSON (JSONDataclass
), the ability to construct CLI argument parsers (ArgparseDataclass
), etc.
This mixin also provides a wrap_dataclass
decorator which can be used to wrap an existing dataclass type into one that provides the mixin's functionality.
Source code in fancy_dataclass/mixin.py
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 236 237 238 239 |
|
__init_subclass__(**kwargs)
classmethod
When inheriting from this class, you may pass various keyword arguments after the list of base classes.
If the base class has a __settings_type__
class attribute (subclass of DataclassMixinSettings
), that class will be instantiated with the provided arguments and stored as a __settings__
attribute on the subclass. These settings can be used to customize the behavior of the subclass.
Additionally, the mixin may set the __field_settings_type__
class attribute to indicate the type (subclass of FieldSettings
) that should be used for field settings, which are extracted from each field's metadata
dict.
Source code in fancy_dataclass/mixin.py
__post_dataclass_wrap__()
classmethod
A hook that is called after the dataclasses.dataclass
decorator is applied to the mixin subclass.
This can be used, for instance, to validate the dataclass fields at definition time.
Source code in fancy_dataclass/mixin.py
_replace(**kwargs)
Constructs a new object with the provided fields modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Any
|
Dataclass fields to modify |
{}
|
Returns:
Type | Description |
---|---|
Self
|
New object with selected fields modified |
Raises:
Type | Description |
---|---|
TypeError
|
If an invalid dataclass field is provided |
Source code in fancy_dataclass/mixin.py
wrap_dataclass(tp, **kwargs)
classmethod
Wraps a dataclass type into a new one which inherits from this mixin class and is otherwise the same.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp |
Type[T]
|
A dataclass type |
required |
kwargs |
Any
|
Keyword arguments to type constructor |
{}
|
Returns:
Type | Description |
---|---|
Type[Self]
|
New dataclass type inheriting from the mixin |
Raises:
Type | Description |
---|---|
TypeError
|
If the given type is not a dataclass |
Source code in fancy_dataclass/mixin.py
DataclassMixinSettings
Bases: DataclassAdaptable
Base class for settings to be associated with fancy_dataclass
mixins.
Each DataclassMixin
class may store a __settings_type__
attribute consisting of a subclass of this class. The settings object will be instantiated as a __settings__
attribute on a mixin subclass when it is defined.
Source code in fancy_dataclass/mixin.py
FieldSettings
Bases: DataclassAdaptable
Class storing a bundle of parameters that will be extracted from dataclass field metadata.
Each DataclassMixin
class may store a __field_settings_type__
attribute which is a FieldSettings
subclass. This specifies which keys in the field.metadata
dictionary are recognized by the mixin class. Other keys will be ignored (unless they are used by other mixin classes).
Source code in fancy_dataclass/mixin.py
from_field(field)
classmethod
Constructs a FieldSettings
object from a dataclasses.Field
's metadata.
Raises:
Type | Description |
---|---|
TypeError
|
If any field has the wrong type |
Source code in fancy_dataclass/mixin.py
type_check()
Checks that every field on the FieldSettings
object is the proper type.
Raises:
Type | Description |
---|---|
TypeError
|
If a field is the wrong type |