mixin
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
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 |
|
__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 MixinSettings
), 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__(wrapped_cls)
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.
NOTE: this function should be idempotent, meaning it can be called multiple times with the same effect. This is because it will be called for every base class of the dataclass
-wrapped class, which may result in duplicate calls.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wrapped_cls |
Type[Self]
|
Class wrapped by the |
required |
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
get_subclass_with_name(typename)
classmethod
Gets the subclass of this class with the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
typename |
str
|
Name of subclass |
required |
Returns:
Type | Description |
---|---|
Type[Self]
|
Subclass with the given name |
Raises:
Type | Description |
---|---|
TypeError
|
If no subclass with the given name exists |
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 |