Skip to content

Commit c65e48c

Browse files
committed
Don't allow checking if None is within the bounds
This was never supported, a `ValueError` would be raised if `None` was passed to `__contains__`. Now we assert to make it clear that it's not allowed. In the future we should probably change `Bounds` to always allow `None` as bounds and make `_T` simply bound to `Comparable`, as supporting `None` depending on the generic type makes it very hard to provide correct type hints. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 1f839e4 commit c65e48c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/frequenz/sdk/timeseries/_base_types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ def __ge__(self, other: Any, /) -> bool:
165165

166166
@dataclass(frozen=True)
167167
class Bounds(Generic[_T]):
168-
"""Lower and upper bound values."""
168+
"""Lower and upper bound values.
169+
170+
Depending on the genertic type `_T`, the lower and upper bounds can be `None`, in
171+
which case it means that there is no lower or upper bound, respectively.
172+
173+
When checking if an item is within the bounds, the item must always be not `None`.
174+
"""
169175

170176
lower: _T
171177
"""Lower bound."""
@@ -178,11 +184,12 @@ def __contains__(self, item: _T) -> bool:
178184
Check if the value is within the range of the container.
179185
180186
Args:
181-
item: The value to check.
187+
item: The value to check. Can't be `None` even if `_T` can be `None`.
182188
183189
Returns:
184190
bool: True if value is within the range, otherwise False.
185191
"""
192+
assert item is not None, "Can't check if `None` is within the bounds."
186193
if self.lower is None and self.upper is None:
187194
return True
188195
if self.lower is None:

0 commit comments

Comments
 (0)