Skip to content

Commit 2e387de

Browse files
authored
Disable pylints no-name-in-module check (#81)
We can disable this rule because `mypy` already checks for this kind of errors and this also fixes a long-standing issue with `pylint` that can't figure out protobuf generate stuff have some members: * pylint-dev/pylint#6281 And now that we disabled this check we can just use the protobuf modules instead of having to declare one alias per each symbol we need. We did this before just so we only need to use a `pylint` `disable=no-name-in-module` one time in the import instead each time we **used** the symbol.
2 parents 8d1c8a8 + 5454cd6 commit 2e387de

9 files changed

+368
-353
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ disable = [
146146
"unsubscriptable-object",
147147
# Checked by mypy
148148
"no-member",
149+
"no-name-in-module",
149150
# Checked by flake8
150151
"f-string-without-interpolation",
151152
"line-too-long",

src/frequenz/client/microgrid/_client.py

+30-41
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@
99
from typing import Any, TypeVar, cast
1010

1111
import grpc.aio
12-
13-
# pylint: disable=no-name-in-module
14-
from frequenz.api.common.components_pb2 import ComponentCategory as PbComponentCategory
15-
from frequenz.api.common.metrics_pb2 import Bounds as PbBounds
16-
from frequenz.api.microgrid.microgrid_pb2 import ComponentData as PbComponentData
17-
from frequenz.api.microgrid.microgrid_pb2 import ComponentFilter as PbComponentFilter
18-
from frequenz.api.microgrid.microgrid_pb2 import ComponentIdParam as PbComponentIdParam
19-
from frequenz.api.microgrid.microgrid_pb2 import ComponentList as PbComponentList
20-
from frequenz.api.microgrid.microgrid_pb2 import ConnectionFilter as PbConnectionFilter
21-
from frequenz.api.microgrid.microgrid_pb2 import ConnectionList as PbConnectionList
22-
from frequenz.api.microgrid.microgrid_pb2 import (
23-
MicrogridMetadata as PbMicrogridMetadata,
24-
)
25-
from frequenz.api.microgrid.microgrid_pb2 import SetBoundsParam as PbSetBoundsParam
26-
from frequenz.api.microgrid.microgrid_pb2 import (
27-
SetPowerActiveParam as PbSetPowerActiveParam,
28-
)
29-
from frequenz.api.microgrid.microgrid_pb2_grpc import MicrogridStub
30-
31-
# pylint: enable=no-name-in-module
12+
from frequenz.api.common import components_pb2, metrics_pb2
13+
from frequenz.api.microgrid import microgrid_pb2, microgrid_pb2_grpc
3214
from frequenz.channels import Receiver
3315
from frequenz.client.base import channel, retry, streaming
34-
from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module
35-
from google.protobuf.timestamp_pb2 import Timestamp # pylint: disable=no-name-in-module
16+
from google.protobuf.empty_pb2 import Empty
17+
from google.protobuf.timestamp_pb2 import Timestamp
3618

3719
from ._component import (
3820
Component,
@@ -87,7 +69,7 @@ def __init__(
8769
self._server_url = server_url
8870
"""The location of the microgrid API server as a URL."""
8971

90-
self.api = MicrogridStub(channel.parse_grpc_uri(server_url))
72+
self.api = microgrid_pb2_grpc.MicrogridStub(channel.parse_grpc_uri(server_url))
9173
"""The gRPC stub for the microgrid API."""
9274

9375
self._broadcasters: dict[int, streaming.GrpcStreamBroadcaster[Any, Any]] = {}
@@ -113,9 +95,9 @@ async def components(self) -> Iterable[Component]:
11395
# grpc.aio is missing types and mypy thinks this is not awaitable,
11496
# but it is
11597
component_list = await cast(
116-
Awaitable[PbComponentList],
98+
Awaitable[microgrid_pb2.ComponentList],
11799
self.api.ListComponents(
118-
PbComponentFilter(),
100+
microgrid_pb2.ComponentFilter(),
119101
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
120102
),
121103
)
@@ -127,7 +109,8 @@ async def components(self) -> Iterable[Component]:
127109
) from grpc_error
128110

129111
components_only = filter(
130-
lambda c: c.category is not PbComponentCategory.COMPONENT_CATEGORY_SENSOR,
112+
lambda c: c.category
113+
is not components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
131114
component_list.components,
132115
)
133116
result: Iterable[Component] = map(
@@ -151,10 +134,10 @@ async def metadata(self) -> Metadata:
151134
Returns:
152135
the microgrid metadata.
153136
"""
154-
microgrid_metadata: PbMicrogridMetadata | None = None
137+
microgrid_metadata: microgrid_pb2.MicrogridMetadata | None = None
155138
try:
156139
microgrid_metadata = await cast(
157-
Awaitable[PbMicrogridMetadata],
140+
Awaitable[microgrid_pb2.MicrogridMetadata],
158141
self.api.GetMicrogridMetadata(
159142
Empty(),
160143
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
@@ -196,14 +179,14 @@ async def connections(
196179
most likely a subclass of
197180
[GrpcError][frequenz.client.microgrid.GrpcError].
198181
"""
199-
connection_filter = PbConnectionFilter(starts=starts, ends=ends)
182+
connection_filter = microgrid_pb2.ConnectionFilter(starts=starts, ends=ends)
200183
try:
201184
valid_components, all_connections = await asyncio.gather(
202185
self.components(),
203186
# grpc.aio is missing types and mypy thinks this is not
204187
# awaitable, but it is
205188
cast(
206-
Awaitable[PbConnectionList],
189+
Awaitable[microgrid_pb2.ConnectionList],
207190
self.api.ListConnections(
208191
connection_filter,
209192
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
@@ -237,7 +220,7 @@ async def _new_component_data_receiver(
237220
*,
238221
component_id: int,
239222
expected_category: ComponentCategory,
240-
transform: Callable[[PbComponentData], _ComponentDataT],
223+
transform: Callable[[microgrid_pb2.ComponentData], _ComponentDataT],
241224
maxsize: int,
242225
) -> Receiver[_ComponentDataT]:
243226
"""Return a new broadcaster receiver for a given `component_id`.
@@ -265,12 +248,14 @@ async def _new_component_data_receiver(
265248
broadcaster = streaming.GrpcStreamBroadcaster(
266249
f"raw-component-data-{component_id}",
267250
# We need to cast here because grpc says StreamComponentData is
268-
# a grpc.CallIterator[PbComponentData] which is not an AsyncIterator,
269-
# but it is a grpc.aio.UnaryStreamCall[..., PbComponentData], which it
270-
# is.
251+
# a grpc.CallIterator[microgrid_pb2.ComponentData] which is not an
252+
# AsyncIterator, but it is a grpc.aio.UnaryStreamCall[...,
253+
# microgrid_pb2.ComponentData], which it is.
271254
lambda: cast(
272-
AsyncIterator[PbComponentData],
273-
self.api.StreamComponentData(PbComponentIdParam(id=component_id)),
255+
AsyncIterator[microgrid_pb2.ComponentData],
256+
self.api.StreamComponentData(
257+
microgrid_pb2.ComponentIdParam(id=component_id)
258+
),
274259
),
275260
transform,
276261
retry_strategy=self._retry_strategy,
@@ -427,7 +412,9 @@ async def set_power(self, component_id: int, power_w: float) -> None:
427412
await cast(
428413
Awaitable[Empty],
429414
self.api.SetPowerActive(
430-
PbSetPowerActiveParam(component_id=component_id, power=power_w),
415+
microgrid_pb2.SetPowerActiveParam(
416+
component_id=component_id, power=power_w
417+
),
431418
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
432419
),
433420
)
@@ -444,7 +431,7 @@ async def set_bounds(
444431
lower: float,
445432
upper: float,
446433
) -> None:
447-
"""Send `PbSetBoundsParam`s received from a channel to the Microgrid service.
434+
"""Send `SetBoundsParam`s received from a channel to the Microgrid service.
448435
449436
Args:
450437
component_id: ID of the component to set bounds for.
@@ -463,15 +450,17 @@ async def set_bounds(
463450
if lower > 0:
464451
raise ValueError(f"Lower bound {lower} must be less than or equal to 0.")
465452

466-
target_metric = PbSetBoundsParam.TargetMetric.TARGET_METRIC_POWER_ACTIVE
453+
target_metric = (
454+
microgrid_pb2.SetBoundsParam.TargetMetric.TARGET_METRIC_POWER_ACTIVE
455+
)
467456
try:
468457
await cast(
469458
Awaitable[Timestamp],
470459
self.api.AddInclusionBounds(
471-
PbSetBoundsParam(
460+
microgrid_pb2.SetBoundsParam(
472461
component_id=component_id,
473462
target_metric=target_metric,
474-
bounds=PbBounds(lower=lower, upper=upper),
463+
bounds=metrics_pb2.Bounds(lower=lower, upper=upper),
475464
),
476465
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
477466
),

src/frequenz/client/microgrid/_component.py

+24-26
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
from dataclasses import dataclass
77
from enum import Enum
88

9-
# pylint: disable=no-name-in-module
10-
from frequenz.api.common.components_pb2 import ComponentCategory as PbComponentCategory
11-
from frequenz.api.microgrid.grid_pb2 import Metadata as PbGridMetadata
12-
from frequenz.api.microgrid.inverter_pb2 import Metadata as PbInverterMetadata
13-
from frequenz.api.microgrid.inverter_pb2 import Type as PbInverterType
14-
15-
# pylint: enable=no-name-in-module
9+
from frequenz.api.common import components_pb2
10+
from frequenz.api.microgrid import grid_pb2, inverter_pb2
1611

1712

1813
class ComponentType(Enum):
@@ -22,22 +17,22 @@ class ComponentType(Enum):
2217
class InverterType(ComponentType):
2318
"""Enum representing inverter types."""
2419

25-
NONE = PbInverterType.TYPE_UNSPECIFIED
20+
NONE = inverter_pb2.Type.TYPE_UNSPECIFIED
2621
"""Unspecified inverter type."""
2722

28-
BATTERY = PbInverterType.TYPE_BATTERY
23+
BATTERY = inverter_pb2.Type.TYPE_BATTERY
2924
"""Battery inverter."""
3025

31-
SOLAR = PbInverterType.TYPE_SOLAR
26+
SOLAR = inverter_pb2.Type.TYPE_SOLAR
3227
"""Solar inverter."""
3328

34-
HYBRID = PbInverterType.TYPE_HYBRID
29+
HYBRID = inverter_pb2.Type.TYPE_HYBRID
3530
"""Hybrid inverter."""
3631

3732

3833
def component_type_from_protobuf(
39-
component_category: PbComponentCategory.ValueType,
40-
component_metadata: PbInverterMetadata,
34+
component_category: components_pb2.ComponentCategory.ValueType,
35+
component_metadata: inverter_pb2.Metadata,
4136
) -> ComponentType | None:
4237
"""Convert a protobuf InverterType message to Component enum.
4338
@@ -53,7 +48,10 @@ def component_type_from_protobuf(
5348
# ComponentType values in the protobuf definition are not unique across categories
5449
# as of v0.11.0, so we need to check the component category first, before doing any
5550
# component type checks.
56-
if component_category == PbComponentCategory.COMPONENT_CATEGORY_INVERTER:
51+
if (
52+
component_category
53+
== components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
54+
):
5755
if not any(int(t.value) == int(component_metadata.type) for t in InverterType):
5856
return None
5957

@@ -65,30 +63,30 @@ def component_type_from_protobuf(
6563
class ComponentCategory(Enum):
6664
"""Possible types of microgrid component."""
6765

68-
NONE = PbComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
66+
NONE = components_pb2.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
6967
"""Unspecified component category."""
7068

71-
GRID = PbComponentCategory.COMPONENT_CATEGORY_GRID
69+
GRID = components_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID
7270
"""Grid component."""
7371

74-
METER = PbComponentCategory.COMPONENT_CATEGORY_METER
72+
METER = components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
7573
"""Meter component."""
7674

77-
INVERTER = PbComponentCategory.COMPONENT_CATEGORY_INVERTER
75+
INVERTER = components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
7876
"""Inverter component."""
7977

80-
BATTERY = PbComponentCategory.COMPONENT_CATEGORY_BATTERY
78+
BATTERY = components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY
8179
"""Battery component."""
8280

83-
EV_CHARGER = PbComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
81+
EV_CHARGER = components_pb2.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
8482
"""EV charger component."""
8583

86-
CHP = PbComponentCategory.COMPONENT_CATEGORY_CHP
84+
CHP = components_pb2.ComponentCategory.COMPONENT_CATEGORY_CHP
8785
"""CHP component."""
8886

8987

9088
def component_category_from_protobuf(
91-
component_category: PbComponentCategory.ValueType,
89+
component_category: components_pb2.ComponentCategory.ValueType,
9290
) -> ComponentCategory:
9391
"""Convert a protobuf ComponentCategory message to ComponentCategory enum.
9492
@@ -105,7 +103,7 @@ def component_category_from_protobuf(
105103
a valid component category as it does not form part of the
106104
microgrid itself)
107105
"""
108-
if component_category == PbComponentCategory.COMPONENT_CATEGORY_SENSOR:
106+
if component_category == components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR:
109107
raise ValueError("Cannot create a component from a sensor!")
110108

111109
if not any(t.value == component_category for t in ComponentCategory):
@@ -136,8 +134,8 @@ class GridMetadata(ComponentMetadata):
136134

137135

138136
def component_metadata_from_protobuf(
139-
component_category: PbComponentCategory.ValueType,
140-
component_metadata: PbGridMetadata,
137+
component_category: components_pb2.ComponentCategory.ValueType,
138+
component_metadata: grid_pb2.Metadata,
141139
) -> GridMetadata | None:
142140
"""Convert a protobuf GridMetadata message to GridMetadata class.
143141
@@ -150,7 +148,7 @@ def component_metadata_from_protobuf(
150148
Returns:
151149
GridMetadata instance corresponding to the protobuf message.
152150
"""
153-
if component_category == PbComponentCategory.COMPONENT_CATEGORY_GRID:
151+
if component_category == components_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID:
154152
max_current = component_metadata.rated_fuse_current
155153
fuse = Fuse(max_current)
156154
return GridMetadata(fuse)

src/frequenz/client/microgrid/_component_data.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
from datetime import datetime, timezone
99
from typing import Self
1010

11-
# pylint: disable=no-name-in-module
12-
from frequenz.api.microgrid.microgrid_pb2 import ComponentData as PbComponentData
11+
from frequenz.api.microgrid import microgrid_pb2
1312

1413
from ._component_error import BatteryError, InverterError
15-
16-
# pylint: enable=no-name-in-module
1714
from ._component_states import (
1815
BatteryComponentState,
1916
BatteryRelayState,
@@ -38,10 +35,10 @@ class ComponentData(ABC):
3835
# data from a protobuf message. The whole protobuf message is stored as the `raw`
3936
# attribute. When `ComponentData` is not instantiated from a protobuf message,
4037
# i.e. using the constructor, `raw` will be set to `None`.
41-
raw: PbComponentData | None = field(default=None, init=False)
38+
raw: microgrid_pb2.ComponentData | None = field(default=None, init=False)
4239
"""Raw component data as decoded from the wire."""
4340

44-
def _set_raw(self, raw: PbComponentData) -> None:
41+
def _set_raw(self, raw: microgrid_pb2.ComponentData) -> None:
4542
"""Store raw protobuf message.
4643
4744
It is preferred to keep the dataclasses immutable (frozen) and make the `raw`
@@ -55,7 +52,7 @@ def _set_raw(self, raw: PbComponentData) -> None:
5552

5653
@classmethod
5754
@abstractmethod
58-
def from_proto(cls, raw: PbComponentData) -> Self:
55+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
5956
"""Create ComponentData from a protobuf message.
6057
6158
Args:
@@ -122,7 +119,7 @@ class MeterData(ComponentData):
122119
"""The AC power frequency in Hertz (Hz)."""
123120

124121
@classmethod
125-
def from_proto(cls, raw: PbComponentData) -> Self:
122+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
126123
"""Create MeterData from a protobuf message.
127124
128125
Args:
@@ -239,7 +236,7 @@ class BatteryData(ComponentData): # pylint: disable=too-many-instance-attribute
239236
"""List of errors in protobuf struct."""
240237

241238
@classmethod
242-
def from_proto(cls, raw: PbComponentData) -> Self:
239+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
243240
"""Create BatteryData from a protobuf message.
244241
245242
Args:
@@ -377,7 +374,7 @@ class InverterData(ComponentData): # pylint: disable=too-many-instance-attribut
377374
"""List of errors from the component."""
378375

379376
@classmethod
380-
def from_proto(cls, raw: PbComponentData) -> Self:
377+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
381378
"""Create InverterData from a protobuf message.
382379
383380
Args:
@@ -533,7 +530,7 @@ class EVChargerData(ComponentData): # pylint: disable=too-many-instance-attribu
533530
"""The state of the ev charger."""
534531

535532
@classmethod
536-
def from_proto(cls, raw: PbComponentData) -> Self:
533+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
537534
"""Create EVChargerData from a protobuf message.
538535
539536
Args:

0 commit comments

Comments
 (0)