Skip to content

Commit b1491be

Browse files
jack-herrmannllucax
authored andcommitted
Add reviewer changes
Signed-off-by: Jack <[email protected]>
1 parent 1435be1 commit b1491be

File tree

2 files changed

+16
-45
lines changed

2 files changed

+16
-45
lines changed

src/frequenz/sdk/timeseries/_base_types.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections.abc import Callable, Iterator
99
from dataclasses import dataclass
1010
from datetime import datetime, timezone
11-
from typing import Any, Generic, Protocol, Self, TypeVar, overload, runtime_checkable
11+
from typing import Any, Generic, Protocol, Self, TypeVar, cast, overload
1212

1313
from ._quantities import Power, QuantityT
1414

@@ -134,40 +134,22 @@ def map(
134134
)
135135

136136

137-
@runtime_checkable
138137
class Comparable(Protocol):
139-
"""
140-
A protocol that requires the implementation of comparison methods.
138+
"""A protocol that requires the implementation of comparison methods.
141139
142140
This protocol is used to ensure that types can be compared using
143141
the less than or equal to (`<=`) and greater than or equal to (`>=`)
144142
operators.
145143
"""
146144

147145
def __le__(self, other: Any, /) -> bool:
148-
"""
149-
Return True if self is less than or equal to other, otherwise False.
150-
151-
Args:
152-
other: The value to compare with.
153-
154-
Returns:
155-
bool: True if self is less than or equal to other, otherwise False.
156-
"""
146+
"""Return whether this instance is less than or equal to `other`."""
157147

158148
def __ge__(self, other: Any, /) -> bool:
159-
"""
160-
Return True if self is greater than or equal to other, otherwise False.
161-
162-
Args:
163-
other: The value to compare with.
164-
165-
Returns:
166-
bool: True if self is greater than or equal to other, otherwise False.
167-
"""
149+
"""Return whether this instance is greater than or equal to `other`."""
168150

169151

170-
_T = TypeVar("_T")
152+
_T = TypeVar("_T", bound=Comparable | None)
171153

172154

173155
@dataclass(frozen=True)
@@ -190,18 +172,14 @@ def __contains__(self, item: _T) -> bool:
190172
Returns:
191173
bool: True if value is within the range, otherwise False.
192174
"""
193-
if item is None:
194-
return False
195-
196-
assert isinstance(item, Comparable)
197-
198-
if self.lower is not None and self.upper is not None:
199-
return self.lower <= item <= self.upper
200-
if self.lower is not None:
201-
return self.lower <= item
202-
if self.upper is not None:
175+
if self.lower is None and self.upper is None:
176+
return True
177+
if self.lower is None:
203178
return item <= self.upper
204-
return False
179+
if self.upper is None:
180+
return self.lower <= item
181+
182+
return cast(Comparable, self.lower) <= item <= cast(Comparable, self.upper)
205183

206184

207185
@dataclass(frozen=True, kw_only=True)
@@ -238,9 +216,7 @@ def __contains__(self, item: Power) -> bool:
238216
Returns:
239217
bool: True if value is within the range, otherwise False.
240218
"""
241-
if not self.inclusion_bounds:
242-
return False
243-
if item not in self.inclusion_bounds:
219+
if not self.inclusion_bounds or item not in self.inclusion_bounds:
244220
return False
245221
if self.exclusion_bounds and item in self.exclusion_bounds:
246222
return False

tests/timeseries/test_base_types.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# License: MIT
2-
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
33

4-
"""Tests for battery pool."""
4+
"""Tests for timeseries base types."""
55

66

77
from datetime import datetime
@@ -11,8 +11,7 @@
1111

1212

1313
def test_bounds_contains() -> None:
14-
"""
15-
Test the `__contains__` method of the `Bounds` class.
14+
"""Test the `__contains__` method of the `Bounds` class.
1615
1716
This method checks if a value is within the defined bounds.
1817
"""
@@ -34,10 +33,6 @@ def test_bounds_contains() -> None:
3433
assert Power.from_watts(10) in bounds_no_upper # at lower bound
3534
assert Power.from_watts(9) not in bounds_no_upper # below lower bound
3635

37-
# test succeeds, mypy 'Unsupported operand types for in ("Power" and "Bounds[None]")'
38-
# bounds_none = Bounds(lower=None, upper=None)
39-
# assert Power.from_watts(15) not in bounds_none
40-
4136

4237
def test_system_bounds_contains() -> None:
4338
"""

0 commit comments

Comments
 (0)