Skip to content

Commit 35f4dce

Browse files
committed
Convert ResamplingFunction to a Protocol
Using a type alias forces us to use strings to specify types because Python can't use forward declarations for statements. Also using a protocol allows for more control, as even when we don't use it for now, it will also check function argument names if `/` is not used. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent b326d4b commit 35f4dce

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/frequenz/sdk/timeseries/_resampling/_config.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from __future__ import annotations
77

88
import logging
9-
from collections.abc import Callable, Sequence
9+
from collections.abc import Sequence
1010
from dataclasses import dataclass
1111
from datetime import datetime, timedelta
12+
from typing import Protocol
1213

1314
from frequenz.quantities import Quantity
1415

@@ -42,32 +43,41 @@
4243
"""
4344

4445

45-
ResamplingFunction = Callable[
46-
[Sequence[Sample[Quantity]], "ResamplerConfig", "SourceProperties"], float
47-
]
48-
"""Resampling function type.
46+
class ResamplingFunction(Protocol):
47+
"""Combine multiple samples into a new one.
4948
50-
A resampling function produces a new sample based on a list of pre-existing
51-
samples. It can do "upsampling" when the data rate of the `input_samples`
52-
period is smaller than the `resampling_period`, or "downsampling" if it is
53-
bigger.
49+
A resampling function produces a new sample based on a list of pre-existing
50+
samples. It can do "upsampling" when the data rate of the `input_samples`
51+
period is smaller than the `resampling_period`, or "downsampling" if it is
52+
bigger.
5453
55-
In general a resampling window is the same as the `resampling_period`, and
56-
this function might receive input samples from multiple windows in the past to
57-
enable extrapolation, but no samples from the future (so the timestamp of the
58-
new sample that is going to be produced will always be bigger than the biggest
59-
timestamp in the input data).
60-
61-
Args:
62-
input_samples (Sequence[Sample]): The sequence of pre-existing samples.
63-
resampler_config (ResamplerConfig): The configuration of the resampling
64-
calling this function.
65-
source_properties (SourceProperties): The properties of the source being
66-
resampled.
54+
In general, a resampling window is the same as the `resampling_period`, and
55+
this function might receive input samples from multiple windows in the past to
56+
enable extrapolation, but no samples from the future (so the timestamp of the
57+
new sample that is going to be produced will always be bigger than the biggest
58+
timestamp in the input data).
59+
"""
6760

68-
Returns:
69-
new_sample (float): The value of new sample produced after the resampling.
70-
"""
61+
def __call__(
62+
self,
63+
input_samples: Sequence[Sample[Quantity]],
64+
resampler_config: ResamplerConfig,
65+
source_properties: SourceProperties,
66+
/,
67+
) -> float:
68+
"""Call the resampling function.
69+
70+
Args:
71+
input_samples: The sequence of pre-existing samples. It must be
72+
non-empty.
73+
resampler_config: The configuration of the resampler calling this
74+
function.
75+
source_properties: The properties of the source being resampled.
76+
77+
Returns:
78+
The value of new sample produced after the resampling.
79+
"""
80+
... # pylint: disable=unnecessary-ellipsis
7181

7282

7383
# pylint: disable=unused-argument

0 commit comments

Comments
 (0)