Skip to content

Commit ba88f89

Browse files
committed
Forbid instantiating Quantity
We don't want Quantity to be instantiated neither by using `zero()` or `from_string()`. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 8a045c2 commit ba88f89

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ def zero(cls) -> Self:
9797
9898
Returns:
9999
A quantity with value 0.0.
100+
101+
Raises:
102+
TypeError: If called on the base
103+
[`Quantity`][frequenz.sdk.timeseries.Quantity] class.
100104
"""
105+
if cls is Quantity:
106+
raise TypeError("Cannot instantiate a base Quantity class.")
107+
101108
_zero = cls._zero_cache.get(cls, None)
102109
if _zero is None:
103110
_zero = cls.__new__(cls)
@@ -118,8 +125,12 @@ def from_string(cls, string: str) -> Self:
118125
119126
Raises:
120127
ValueError: If the string does not match the expected format.
121-
128+
TypeError: If called on the base
129+
[`Quantity`][frequenz.sdk.timeseries.Quantity] class.
122130
"""
131+
if cls is Quantity:
132+
raise TypeError("Cannot instantiate a base Quantity class.")
133+
123134
split_string = string.split(" ")
124135

125136
if len(split_string) != 2:

0 commit comments

Comments
 (0)