Skip to content

Commit

Permalink
Don't allow checking if None is within the bounds (#1140)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
llucax authored Jan 3, 2025
2 parents 6401e83 + 382bfcb commit fcb7de4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/frequenz/sdk/timeseries/_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ def __ge__(self, other: Any, /) -> bool:

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

lower: _T
"""Lower bound."""
Expand All @@ -178,11 +184,12 @@ def __contains__(self, item: _T) -> bool:
Check if the value is within the range of the container.
Args:
item: The value to check.
item: The value to check. Can't be `None` even if `_T` can be `None`.
Returns:
bool: True if value is within the range, otherwise False.
"""
assert item is not None, "Can't check if `None` is within the bounds."
if self.lower is None and self.upper is None:
return True
if self.lower is None:
Expand Down

0 comments on commit fcb7de4

Please sign in to comment.