33
44"""Module to define the client class."""
55
6+ from __future__ import annotations
7+
68import logging
79from datetime import datetime , timezone
810from decimal import Decimal , InvalidOperation
9- from typing import Awaitable , cast
11+ from typing import TYPE_CHECKING , Any , Awaitable , cast
1012
1113import grpc
1214
1315# pylint: disable=no-member
1416from frequenz .api .electricity_trading .v1 import electricity_trading_pb2
1517from frequenz .api .electricity_trading .v1 .electricity_trading_pb2_grpc import (
16- ElectricityTradingServiceAsyncStub ,
1718 ElectricityTradingServiceStub ,
1819)
1920from frequenz .channels import Receiver
4344 UpdateOrder ,
4445)
4546
47+ if TYPE_CHECKING :
48+ from frequenz .api .electricity_trading .v1 .electricity_trading_pb2_grpc import (
49+ ElectricityTradingServiceAsyncStub ,
50+ )
51+
52+
4653_logger = logging .getLogger (__name__ )
4754
4855
@@ -126,10 +133,9 @@ def __init__(
126133 self , "_initialized"
127134 ): # Prevent re-initialization of existing instances
128135 super ().__init__ (server_url , connect = connect )
129- self ._stub = cast (
130- ElectricityTradingServiceAsyncStub ,
131- ElectricityTradingServiceStub (self .channel ),
132- )
136+ self ._stub : ElectricityTradingServiceAsyncStub | None = None
137+ if connect :
138+ self ._create_stub ()
133139 self ._initialized = True
134140
135141 self ._gridpool_orders_streams : dict [
@@ -155,6 +161,11 @@ def __init__(
155161
156162 self ._metadata = (("key" , auth_key ),) if auth_key else ()
157163
164+ def _create_stub (self ) -> None :
165+ """Create a new gRPC stub for the Electricity Trading service."""
166+ stub : Any = ElectricityTradingServiceStub (self .channel )
167+ self ._stub = stub
168+
158169 @property
159170 def stub (self ) -> ElectricityTradingServiceAsyncStub :
160171 """
0 commit comments