Skip to content

Commit 5a03dba

Browse files
committed
Use ParamSpec to not lose typing info in the from_pb decorator
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent fc0b4a5 commit 5a03dba

File tree

1 file changed

+8
-5
lines changed
  • src/frequenz/client/electricity_trading

1 file changed

+8
-5
lines changed

src/frequenz/client/electricity_trading/_types.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from datetime import datetime, timedelta, timezone
1414
from decimal import Decimal
1515
from functools import wraps
16-
from typing import Any, Callable, Self, Type, TypeVar
16+
from typing import Callable, Concatenate, ParamSpec, Self, TypeVar
1717

1818
# pylint: disable=no-member
1919
from frequenz.api.common.v1.grid import delivery_area_pb2, delivery_duration_pb2
@@ -26,9 +26,12 @@
2626

2727

2828
T = TypeVar("T") # Generic type variable for class methods
29+
P = ParamSpec("P")
2930

3031

31-
def from_pb(func: Callable[[Type[T], Any], T]) -> Callable[[Type[T], Any], T]:
32+
def from_pb(
33+
func: Callable[Concatenate[type[T], P], T]
34+
) -> Callable[Concatenate[type[T], P], T]:
3235
"""Standardize from_pb methods like error handling with this decorator.
3336
3437
Args:
@@ -39,12 +42,12 @@ def from_pb(func: Callable[[Type[T], Any], T]) -> Callable[[Type[T], Any], T]:
3942
"""
4043

4144
@wraps(func)
42-
def wrapper(cls: Type[T], pb_obj: Any) -> T:
45+
def wrapper(cls: type[T], /, *args: P.args, **kwargs: P.kwargs) -> T:
4346
try:
44-
return func(cls, pb_obj)
47+
return func(cls, *args, **kwargs)
4548
except Exception as e:
4649
_logger.error(
47-
"Error converting %s from protobuf (`%s`): %s", cls.__name__, pb_obj, e
50+
"Error converting %s from protobuf (`%s`): %s", cls.__name__, *args, e
4851
)
4952
raise
5053

0 commit comments

Comments
 (0)