Skip to content

Commit 7f14064

Browse files
committed
Return Self in from_proto() constructors
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent bbdced7 commit 7f14064

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/frequenz/client/microgrid/_component_data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

44
"""Component data types for data coming from a microgrid."""
5-
from __future__ import annotations
65

76
from abc import ABC, abstractmethod
87
from dataclasses import dataclass, field
98
from datetime import datetime, timezone
9+
from typing import Self
1010

1111
# pylint: disable=no-name-in-module
1212
from frequenz.api.microgrid.battery_pb2 import ComponentState as PbBatteryComponentState
@@ -54,7 +54,7 @@ def _set_raw(self, raw: PbComponentData) -> None:
5454

5555
@classmethod
5656
@abstractmethod
57-
def from_proto(cls, raw: PbComponentData) -> ComponentData:
57+
def from_proto(cls, raw: PbComponentData) -> Self:
5858
"""Create ComponentData from a protobuf message.
5959
6060
Args:
@@ -93,7 +93,7 @@ class MeterData(ComponentData):
9393
"""The AC power frequency in Hertz (Hz)."""
9494

9595
@classmethod
96-
def from_proto(cls, raw: PbComponentData) -> MeterData:
96+
def from_proto(cls, raw: PbComponentData) -> Self:
9797
"""Create MeterData from a protobuf message.
9898
9999
Args:
@@ -206,7 +206,7 @@ class BatteryData(ComponentData): # pylint: disable=too-many-instance-attribute
206206
"""List of errors in protobuf struct."""
207207

208208
@classmethod
209-
def from_proto(cls, raw: PbComponentData) -> BatteryData:
209+
def from_proto(cls, raw: PbComponentData) -> Self:
210210
"""Create BatteryData from a protobuf message.
211211
212212
Args:
@@ -316,7 +316,7 @@ class InverterData(ComponentData): # pylint: disable=too-many-instance-attribut
316316
"""List of errors from the component."""
317317

318318
@classmethod
319-
def from_proto(cls, raw: PbComponentData) -> InverterData:
319+
def from_proto(cls, raw: PbComponentData) -> Self:
320320
"""Create InverterData from a protobuf message.
321321
322322
Args:
@@ -436,7 +436,7 @@ class EVChargerData(ComponentData): # pylint: disable=too-many-instance-attribu
436436
"""The state of the ev charger."""
437437

438438
@classmethod
439-
def from_proto(cls, raw: PbComponentData) -> EVChargerData:
439+
def from_proto(cls, raw: PbComponentData) -> Self:
440440
"""Create EVChargerData from a protobuf message.
441441
442442
Args:

src/frequenz/client/microgrid/_component_states.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

44
"""Defines states of components that can be used in a microgrid."""
5-
from __future__ import annotations
65

76
from enum import Enum
7+
from typing import Self
88

99
# pylint: disable=no-name-in-module
1010
from frequenz.api.microgrid.ev_charger_pb2 import CableState as PbCableState
@@ -35,7 +35,7 @@ class EVChargerCableState(Enum):
3535
"""The cable is plugged into the EV and locked."""
3636

3737
@classmethod
38-
def from_pb(cls, evc_state: PbCableState.ValueType) -> EVChargerCableState:
38+
def from_pb(cls, evc_state: PbCableState.ValueType) -> Self:
3939
"""Convert a protobuf CableState value to EVChargerCableState enum.
4040
4141
Args:
@@ -45,9 +45,9 @@ def from_pb(cls, evc_state: PbCableState.ValueType) -> EVChargerCableState:
4545
Enum value corresponding to the protobuf message.
4646
"""
4747
if not any(t.value == evc_state for t in EVChargerCableState):
48-
return cls.UNSPECIFIED
48+
return cls(cls.UNSPECIFIED)
4949

50-
return EVChargerCableState(evc_state)
50+
return cls(evc_state)
5151

5252

5353
class EVChargerComponentState(Enum):
@@ -81,7 +81,7 @@ class EVChargerComponentState(Enum):
8181
"""The component is interrupted."""
8282

8383
@classmethod
84-
def from_pb(cls, evc_state: PbComponentState.ValueType) -> EVChargerComponentState:
84+
def from_pb(cls, evc_state: PbComponentState.ValueType) -> Self:
8585
"""Convert a protobuf ComponentState value to EVChargerComponentState enum.
8686
8787
Args:
@@ -91,6 +91,6 @@ def from_pb(cls, evc_state: PbComponentState.ValueType) -> EVChargerComponentSta
9191
Enum value corresponding to the protobuf message.
9292
"""
9393
if not any(t.value == evc_state for t in EVChargerComponentState):
94-
return cls.UNSPECIFIED
94+
return cls(cls.UNSPECIFIED)
9595

96-
return EVChargerComponentState(evc_state)
96+
return cls(evc_state)

src/frequenz/client/microgrid/_retry.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
"""Implementations for retry strategies."""
55

6-
from __future__ import annotations
7-
86
import random
97
from abc import ABC, abstractmethod
108
from collections.abc import Iterator
119
from copy import deepcopy
10+
from typing import Self
1211

1312
DEFAULT_RETRY_INTERVAL = 3.0
1413
"""Default retry interval, in seconds."""
@@ -52,7 +51,7 @@ def reset(self) -> None:
5251
"""
5352
self._count = 0
5453

55-
def copy(self) -> RetryStrategy:
54+
def copy(self) -> Self:
5655
"""Create a new instance of `self`.
5756
5857
Returns:

0 commit comments

Comments
 (0)