44"""Custom marshmallow fields and schema.
55
66This module provides custom marshmallow fields for quantities and
7- a [`QuantitySchema`][.QuantitySchema] class to
7+ a [`QuantitySchema`][frequenz.quantities.experimental.marshmallow .QuantitySchema] class to
88be used as base schema for dataclasses containing quantities.
99
1010Danger:
4343
4444
4545class _QuantityField (Field [Quantity ]):
46- """A custom field for [`Quantity`][... .Quantity] objects.
46+ """A custom field for [`Quantity`][frequenz.quantities .Quantity] objects.
4747
4848 Supports per-field serialization configuration.
4949
5050 This class handles serialization and deserialization of ALL
51- [`Quantity`][....Quantity] subclasses.
52- The specific [`Quantity`][....Quantity] subclass is determined by the
53- [`.field_type`][.field_type] attribute.
51+ [`Quantity`][frequenz.quantities.Quantity] subclasses.
52+ The specific [`Quantity`][frequenz.quantities.Quantity] subclass is determined by the
53+ [`.field_type`][frequenz.quantities.experimental.marshmallow._QuantityField.field_type]
54+ attribute.
5455
5556 * Deserialization auto-detects the type of deserialization (float or string)
5657 based on the input type.
5758 * Serialization uses either the schema's default or the per-field
5859 configuration found in the metadata.
5960
6061 We need distinct `_QuantityField` subclasses for each
61- [`Quantity`][... .Quantity] subclass, so
62- they can be used in the [`TYPE_MAPPING`][. .QuantitySchema.TYPE_MAPPING] in
63- [`QuantitySchema`][. .QuantitySchema].
62+ [`Quantity`][frequenz.quantities .Quantity] subclass, so
63+ they can be used in the [`TYPE_MAPPING`][frequenz.quantities.experimental.marshmallow .QuantitySchema.TYPE_MAPPING] in
64+ [`QuantitySchema`][frequenz.quantities.experimental.marshmallow .QuantitySchema].
6465 This class is not intended to be used directly.
6566
6667 Instead, we use the specific `_QuantityField` subclasses for each
67- [`Quantity`][... .Quantity].
68- Each field subclass simply sets the [`.field_type`][.field_type]
69- attribute to the corresponding [`Quantity`][... .Quantity] subclass.
68+ [`Quantity`][frequenz.quantities .Quantity].
69+ Each field subclass simply sets the [`.field_type`][frequenz.quantities.experimental.marshmallow._QuantityField .field_type]
70+ attribute to the corresponding [`Quantity`][frequenz.quantities .Quantity] subclass.
7071
71- Those subclasses are stored in [`QUANTITY_FIELD_CLASSES`][. .QUANTITY_FIELD_CLASSES]
72- and are used for the [`TYPE_MAPPING`][. .QuantitySchema.TYPE_MAPPING] in
73- [`QuantitySchema`][. .QuantitySchema].
72+ Those subclasses are stored in [`QUANTITY_FIELD_CLASSES`][frequenz.quantities.experimental.marshmallow .QUANTITY_FIELD_CLASSES]
73+ and are used for the [`TYPE_MAPPING`][frequenz.quantities.experimental.marshmallow .QuantitySchema.TYPE_MAPPING] in
74+ [`QuantitySchema`][frequenz.quantities.experimental.marshmallow .QuantitySchema].
7475 """
7576
7677 field_type : Type [Quantity ] | None = None
77- """The specific [`Quantity`][.... .Quantity] subclass."""
78+ """The specific [`Quantity`][frequenz.quantities .Quantity] subclass."""
7879
7980 def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
8081 """Initialize the field."""
@@ -84,7 +85,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
8485 def _serialize (
8586 self , value : Quantity | None , attr : str | None , obj : Any , ** kwargs : Any
8687 ) -> Any :
87- """Serialize a [`Quantity`][.... .Quantity] based on per-field configuration.
88+ """Serialize a [`Quantity`][frequenz.quantities .Quantity] based on per-field configuration.
8889
8990 Args:
9091 value: The quantity to serialize, or `None`.
@@ -97,9 +98,9 @@ def _serialize(
9798 the raw base float value otherwise. `None` if `value` is `None`.
9899
99100 Raises:
100- TypeError: If [`.. field_type`][. .field_type] is not set to a
101- [`Quantity`][.... .Quantity] subclass, or if
102- `value` is not a [`Quantity`][.... .Quantity]
101+ TypeError: If [`.field_type`][frequenz.quantities.experimental.marshmallow._QuantityField .field_type] is not set to a
102+ [`Quantity`][frequenz.quantities .Quantity] subclass, or if
103+ `value` is not a [`Quantity`][frequenz.quantities .Quantity]
103104 instance.
104105 """
105106 if self .field_type is None or not issubclass (self .field_type , Quantity ):
@@ -132,7 +133,7 @@ def _serialize(
132133 def _deserialize (
133134 self , value : Any , attr : str | None , data : Any , ** kwargs : Any
134135 ) -> Quantity :
135- """Deserialize a [`Quantity`][.... .Quantity] from a float, int, or string.
136+ """Deserialize a [`Quantity`][frequenz.quantities .Quantity] from a float, int, or string.
136137
137138 Args:
138139 value: The raw value to deserialize (float, int, or string).
@@ -144,8 +145,8 @@ def _deserialize(
144145 The deserialized quantity instance.
145146
146147 Raises:
147- TypeError: If [`.. field_type`][. .field_type] is not set to a
148- [`Quantity`][.... .Quantity] subclass.
148+ TypeError: If [`.field_type`][frequenz.quantities.experimental.marshmallow._QuantityField .field_type] is not set to a
149+ [`Quantity`][frequenz.quantities .Quantity] subclass.
149150 ValidationError: If the input type is invalid or parsing fails
150151 (see [`marshmallow.ValidationError`][marshmallow.ValidationError]).
151152 """
@@ -186,55 +187,55 @@ def _deserialize(
186187
187188
188189class ApparentPowerField (_QuantityField ):
189- """A custom field for [`ApparentPower`][... .ApparentPower] objects."""
190+ """A custom field for [`ApparentPower`][frequenz.quantities .ApparentPower] objects."""
190191
191192 field_type = ApparentPower
192193
193194
194195class CurrentField (_QuantityField ):
195- """A custom field for [`Current`][... .Current] objects."""
196+ """A custom field for [`Current`][frequenz.quantities .Current] objects."""
196197
197198 field_type = Current
198199
199200
200201class EnergyField (_QuantityField ):
201- """A custom field for [`Energy`][... .Energy] objects."""
202+ """A custom field for [`Energy`][frequenz.quantities .Energy] objects."""
202203
203204 field_type = Energy
204205
205206
206207class FrequencyField (_QuantityField ):
207- """A custom field for [`Frequency`][... .Frequency] objects."""
208+ """A custom field for [`Frequency`][frequenz.quantities .Frequency] objects."""
208209
209210 field_type = Frequency
210211
211212
212213class PercentageField (_QuantityField ):
213- """A custom field for [`Percentage`][... .Percentage] objects."""
214+ """A custom field for [`Percentage`][frequenz.quantities .Percentage] objects."""
214215
215216 field_type = Percentage
216217
217218
218219class PowerField (_QuantityField ):
219- """A custom field for [`Power`][... .Power] objects."""
220+ """A custom field for [`Power`][frequenz.quantities .Power] objects."""
220221
221222 field_type = Power
222223
223224
224225class ReactivePowerField (_QuantityField ):
225- """A custom field for [`ReactivePower`][... .ReactivePower] objects."""
226+ """A custom field for [`ReactivePower`][frequenz.quantities .ReactivePower] objects."""
226227
227228 field_type = ReactivePower
228229
229230
230231class TemperatureField (_QuantityField ):
231- """A custom field for [`Temperature`][... .Temperature] objects."""
232+ """A custom field for [`Temperature`][frequenz.quantities .Temperature] objects."""
232233
233234 field_type = Temperature
234235
235236
236237class VoltageField (_QuantityField ):
237- """A custom field for [`Voltage`][... .Voltage] objects."""
238+ """A custom field for [`Voltage`][frequenz.quantities .Voltage] objects."""
238239
239240 field_type = Voltage
240241
@@ -250,10 +251,10 @@ class VoltageField(_QuantityField):
250251 Temperature : TemperatureField ,
251252 Voltage : VoltageField ,
252253}
253- """The mapping from [`Quantity`][... .Quantity] subclasses to their corresponding field subclasses.
254+ """The mapping from [`Quantity`][frequenz.quantities .Quantity] subclasses to their corresponding field subclasses.
254255
255- This mapping is used in [`QuantitySchema.TYPE_MAPPING`][. .QuantitySchema.TYPE_MAPPING] to
256- determine the correct field class for each [`Quantity`][... .Quantity]
256+ This mapping is used in [`QuantitySchema.TYPE_MAPPING`][frequenz.quantities.experimental.marshmallow .QuantitySchema.TYPE_MAPPING] to
257+ determine the correct field class for each [`Quantity`][frequenz.quantities .Quantity]
257258subclass.
258259"""
259260
@@ -327,4 +328,4 @@ class Config:
327328 """
328329
329330 TYPE_MAPPING : dict [type , type [Field [Any ]]] = QUANTITY_FIELD_CLASSES
330- """The field class to use for each [`Quantity`][.... .Quantity] subclass."""
331+ """The field class to use for each [`Quantity`][frequenz.quantities .Quantity] subclass."""
0 commit comments