Skip to content

Commit 3d6cd6e

Browse files
committed
Replace HOFormulaBuilder @overloads with Self type
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent cde6102 commit 3d6cd6e

File tree

1 file changed

+10
-134
lines changed

1 file changed

+10
-134
lines changed

src/frequenz/sdk/timeseries/formula_engine/_formula_engine.py

Lines changed: 10 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from abc import ABC
1313
from collections import deque
1414
from collections.abc import Callable
15-
from typing import Generic, Union, overload
15+
from typing import Generic, Self, Union
1616

1717
from frequenz.channels import Broadcast, Receiver
1818

@@ -68,16 +68,6 @@
6868
"HigherOrderFormulaBuilder3Phase", # type: ignore[type-arg]
6969
]
7070

71-
_CompositionType1Phase = Union[
72-
"FormulaEngine", # type: ignore[type-arg]
73-
"HigherOrderFormulaBuilder", # type: ignore[type-arg]
74-
]
75-
76-
_CompositionType3Phase = Union[
77-
"FormulaEngine3Phase", # type: ignore[type-arg]
78-
"HigherOrderFormulaBuilder3Phase", # type: ignore[type-arg]
79-
]
80-
8171

8272
class FormulaEngine(Generic[QuantityT]):
8373
"""[`FormulaEngine`][frequenz.sdk.timeseries.formula_engine.FormulaEngine]s are a
@@ -908,22 +898,7 @@ def __init__(
908898
self._steps.append((TokenType.COMPONENT_METRIC, engine))
909899
self._create_method: Callable[[float], QuantityT] = create_method
910900

911-
@overload
912-
def _push(
913-
self, oper: str, other: _CompositionType1Phase
914-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
915-
916-
@overload
917-
def _push(
918-
self, oper: str, other: _CompositionType3Phase | QuantityT | float
919-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
920-
921-
def _push(
922-
self, oper: str, other: _CompositionType | QuantityT | float
923-
) -> (
924-
HigherOrderFormulaBuilder[QuantityT]
925-
| HigherOrderFormulaBuilder3Phase[QuantityT]
926-
):
901+
def _push(self, oper: str, other: _CompositionType | QuantityT | float) -> Self:
927902
self._steps.appendleft((TokenType.OPER, "("))
928903
self._steps.append((TokenType.OPER, ")"))
929904
self._steps.append((TokenType.OPER, oper))
@@ -950,27 +925,9 @@ def _push(
950925
self._steps.append((TokenType.OPER, ")"))
951926
else:
952927
raise RuntimeError(f"Can't build a formula from: {other}")
953-
assert isinstance(
954-
self, (HigherOrderFormulaBuilder, HigherOrderFormulaBuilder3Phase)
955-
)
956928
return self
957929

958-
@overload
959-
def __add__(
960-
self, other: _CompositionType1Phase | QuantityT
961-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
962-
963-
@overload
964-
def __add__(
965-
self, other: _CompositionType3Phase
966-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
967-
968-
def __add__(
969-
self, other: _CompositionType | QuantityT
970-
) -> (
971-
HigherOrderFormulaBuilder[QuantityT]
972-
| HigherOrderFormulaBuilder3Phase[QuantityT]
973-
):
930+
def __add__(self, other: _CompositionType | QuantityT) -> Self:
974931
"""Return a formula builder that adds (data in) `other` to `self`.
975932
976933
Args:
@@ -983,23 +940,10 @@ def __add__(
983940
"""
984941
return self._push("+", other)
985942

986-
@overload
987-
def __sub__(
988-
self, other: _CompositionType1Phase | QuantityT
989-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
990-
991-
@overload
992-
def __sub__(
993-
self, other: _CompositionType3Phase
994-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
995-
996943
def __sub__(
997944
self,
998945
other: _CompositionType | QuantityT,
999-
) -> (
1000-
HigherOrderFormulaBuilder[QuantityT]
1001-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1002-
):
946+
) -> Self:
1003947
"""Return a formula builder that subtracts (data in) `other` from `self`.
1004948
1005949
Args:
@@ -1012,23 +956,10 @@ def __sub__(
1012956
"""
1013957
return self._push("-", other)
1014958

1015-
@overload
1016-
def __mul__(
1017-
self, other: _CompositionType1Phase | float
1018-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
1019-
1020-
@overload
1021-
def __mul__(
1022-
self, other: _CompositionType3Phase
1023-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
1024-
1025959
def __mul__(
1026960
self,
1027961
other: _CompositionType | float,
1028-
) -> (
1029-
HigherOrderFormulaBuilder[QuantityT]
1030-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1031-
):
962+
) -> Self:
1032963
"""Return a formula builder that multiplies (data in) `self` with `other`.
1033964
1034965
Args:
@@ -1041,23 +972,10 @@ def __mul__(
1041972
"""
1042973
return self._push("*", other)
1043974

1044-
@overload
1045-
def __truediv__(
1046-
self, other: _CompositionType1Phase | float
1047-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
1048-
1049-
@overload
1050-
def __truediv__(
1051-
self, other: _CompositionType3Phase
1052-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
1053-
1054975
def __truediv__(
1055976
self,
1056977
other: _CompositionType | float,
1057-
) -> (
1058-
HigherOrderFormulaBuilder[QuantityT]
1059-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1060-
):
978+
) -> Self:
1061979
"""Return a formula builder that divides (data in) `self` by `other`.
1062980
1063981
Args:
@@ -1070,22 +988,7 @@ def __truediv__(
1070988
"""
1071989
return self._push("/", other)
1072990

1073-
@overload
1074-
def max(
1075-
self, other: _CompositionType1Phase | QuantityT
1076-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
1077-
1078-
@overload
1079-
def max(
1080-
self, other: _CompositionType3Phase
1081-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
1082-
1083-
def max(
1084-
self, other: _CompositionType | QuantityT
1085-
) -> (
1086-
HigherOrderFormulaBuilder[QuantityT]
1087-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1088-
):
991+
def max(self, other: _CompositionType | QuantityT) -> Self:
1089992
"""Return a formula builder that calculates the maximum of `self` and `other`.
1090993
1091994
Args:
@@ -1098,22 +1001,7 @@ def max(
10981001
"""
10991002
return self._push("max", other)
11001003

1101-
@overload
1102-
def min(
1103-
self, other: _CompositionType1Phase | QuantityT
1104-
) -> HigherOrderFormulaBuilder[QuantityT]: ...
1105-
1106-
@overload
1107-
def min(
1108-
self, other: _CompositionType3Phase
1109-
) -> HigherOrderFormulaBuilder3Phase[QuantityT]: ...
1110-
1111-
def min(
1112-
self, other: _CompositionType | QuantityT
1113-
) -> (
1114-
HigherOrderFormulaBuilder[QuantityT]
1115-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1116-
):
1004+
def min(self, other: _CompositionType | QuantityT) -> Self:
11171005
"""Return a formula builder that calculates the minimum of `self` and `other`.
11181006
11191007
Args:
@@ -1128,10 +1016,7 @@ def min(
11281016

11291017
def consumption(
11301018
self,
1131-
) -> (
1132-
HigherOrderFormulaBuilder[QuantityT]
1133-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1134-
):
1019+
) -> Self:
11351020
"""Apply the Consumption Operator.
11361021
11371022
The consumption operator returns either the identity if the power value is
@@ -1144,17 +1029,11 @@ def consumption(
11441029
self._steps.appendleft((TokenType.OPER, "("))
11451030
self._steps.append((TokenType.OPER, ")"))
11461031
self._steps.append((TokenType.OPER, "consumption"))
1147-
assert isinstance(
1148-
self, (HigherOrderFormulaBuilder, HigherOrderFormulaBuilder3Phase)
1149-
)
11501032
return self
11511033

11521034
def production(
11531035
self,
1154-
) -> (
1155-
HigherOrderFormulaBuilder[QuantityT]
1156-
| HigherOrderFormulaBuilder3Phase[QuantityT]
1157-
):
1036+
) -> Self:
11581037
"""Apply the Production Operator.
11591038
11601039
The production operator returns either the absolute value if the power value is
@@ -1167,9 +1046,6 @@ def production(
11671046
self._steps.appendleft((TokenType.OPER, "("))
11681047
self._steps.append((TokenType.OPER, ")"))
11691048
self._steps.append((TokenType.OPER, "production"))
1170-
assert isinstance(
1171-
self, (HigherOrderFormulaBuilder, HigherOrderFormulaBuilder3Phase)
1172-
)
11731049
return self
11741050

11751051

0 commit comments

Comments
 (0)