Skip to content

math: Make Interval and IntervalSet covariant#180

Draft
llucax wants to merge 3 commits into
frequenz-floss:v1.x.xfrom
llucax:interval-covariance
Draft

math: Make Interval and IntervalSet covariant#180
llucax wants to merge 3 commits into
frequenz-floss:v1.x.xfrom
llucax:interval-covariance

Conversation

@llucax

@llucax llucax commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Interval and IntervalSet are immutable (frozen) containers whose value type only ever appears in output positions: the start / end fields, the stored intervals, and iteration. Their type parameter can therefore be covariant, so an Interval[T] / IntervalSet[T] is accepted where an Interval[S] / IntervalSet[S] is expected when T is a subtype of S (for example, passing an Interval[Power] where an Interval[Quantity] is wanted). The previous invariant typing rejected these safe assignments for no benefit; contravariance is unsound, as it would allow a stored S to be read back as a T.

A covariant type variable must not appear in any input position, so __contains__ now takes object instead of the value type, following the convention used by the standard library containers (Container, Sequence, ...). The item is cast back to the value type internally, so runtime behaviour is unchanged: comparing a value that is not comparable to the bounds still raises TypeError.

This reverts the soft breaking change from "Remove None from Interval type parameter": with __contains__ widened to object, None in some_interval (and other incompatible membership checks) are no longer flagged by type checkers at call sites. Losing that call-site check is the accepted trade-off for covariance, and misuse still fails loudly at runtime. The release notes are updated accordingly.

Add tests for both aspects. The covariance tests assign an Interval[bool] / IntervalSet[bool] to the int variant; these only type check under covariance and are enforced by mypy, since the test suite is type checked (they are no-ops at runtime). The runtime tests assert that a membership check with a value that can't be compared to the bounds raises TypeError.

llucax added 3 commits July 3, 2026 10:49
`Interval`'s type parameter no longer includes `None`. The exported type
variable `LessThanComparableOrNoneT` (bound to
`LessThanComparable | None`) was deprecated and replaced with
`LessThanComparableT` (bound to `LessThanComparable`).

`None` is still accepted as a value for `start` / `end` to indicate an
unbounded side, but it is treated purely as bound metadata rather than
a value in the interval's comparable space.

Note that explicitly using `Interval[int | None]` still works, as
`(int | None) | None` is equivalent to `int | None`, even when it is no
longer needed nor recommended. Old code will mostly work, but there is
one soft breaking change: `None in some_interval` and
`None in some_interval_set` is now a type-check error at call sites,
matching the design intent that `None` is a bound marker and never a
member value.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Add IntervalSet[T] to frequenz.core.math: a normalized, immutable set of
Interval[T] values that supports O(log n) membership testing via the `in`
operator.

On construction, IntervalSet sorts the passed intervals by start and
merges any that overlap or share an inclusive endpoint. Only the
normalized form is stored, so equality, hashing, and iteration are
well-defined regardless of input order; callers that need the original
inputs can keep them separately.

Unbounded intervals are handled via the existing Interval[T] convention:
a None start is treated as -infinity and a None end as +infinity during
sort/merge, so for example IntervalSet((Interval(None, 5), Interval(3,
None))) collapses to a single Interval(None, None).

The generic container-of-containers variant discussed during design is
deferred: there is no immediate use case, and adding IntervalSet alone
is a fully additive step that does not foreclose a future generic type
in a separate module.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
`Interval` and `IntervalSet` are immutable (frozen) containers whose
value type only ever appears in output positions: the `start` / `end`
fields, the stored `intervals`, and iteration. Their type parameter can
therefore be covariant, so an `Interval[T]` / `IntervalSet[T]` is
accepted where an `Interval[S]` / `IntervalSet[S]` is expected when `T`
is a subtype of `S` (for example, passing an `Interval[Power]` where an
`Interval[Quantity]` is wanted). The previous invariant typing rejected
these safe assignments for no benefit; contravariance is unsound, as it
would allow a stored `S` to be read back as a `T`.

A covariant type variable must not appear in any input position, so
`__contains__` now takes `object` instead of the value type, following
the convention used by the standard library containers (`Container`,
`Sequence`, ...). The item is cast back to the value type internally, so
runtime behaviour is unchanged: comparing a value that is not comparable
to the bounds still raises `TypeError`.

This reverts the soft breaking change from "Remove `None` from
`Interval` type parameter": with `__contains__` widened to `object`,
`None in some_interval` (and other incompatible membership checks) are
no longer flagged by type checkers at call sites. Losing that call-site
check is the accepted trade-off for covariance, and misuse still fails
loudly at runtime. The release notes are updated accordingly.

Add tests for both aspects. The covariance tests assign an
`Interval[bool]` / `IntervalSet[bool]` to the `int` variant; these only
type check under covariance and are enforced by mypy, since the test
suite is type checked (they are no-ops at runtime). The runtime tests
assert that a membership check with a value that can't be compared to
the bounds raises `TypeError`.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax llucax requested a review from a team as a code owner July 3, 2026 12:51
@llucax llucax requested review from florian-wagner-frequenz and removed request for a team July 3, 2026 12:51
@llucax llucax marked this pull request as draft July 3, 2026 12:51
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:math Affects the math module labels Jul 3, 2026
@llucax llucax changed the title interval covariance math: Make Interval and IntervalSet covariant Jul 3, 2026
@llucax

llucax commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Draft because it is based on #179.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:math Affects the math module part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant