Skip to content

Add a FloatInt (float | int) type alias - #189

Open
llucax wants to merge 3 commits into
frequenz-floss:v1.x.xfrom
llucax:float-int
Open

Add a FloatInt (float | int) type alias#189
llucax wants to merge 3 commits into
frequenz-floss:v1.x.xfrom
llucax:float-int

Conversation

@llucax

@llucax llucax commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

This is a port of the FloatInt type alias in he full analysis is recorded in frequenz-floss/frequenz-client-common-python#181. Refer to frequenz-floss/frequenz-client-common-python#250 for details.

Fixes #181.

llucax added 3 commits August 13, 2026 13:03
PEP 484's numeric tower makes `int` assignable to any `float`-annotated
parameter, attribute or variable, even under `mypy --strict`, while at
runtime `isinstance(1, float)` is `False`. A plain `float` annotation is
therefore a lie: it silently admits values that fall through an
apparently exhaustive `match … case float():` into `assert_never()`, and
that lack `float`-only methods like `hex()`.

There is no clean fix in Python, and the alternatives were all measured
or analyzed and rejected: coercing at ingress costs ~2.3x on the
construction of hot-path types, structural `Protocol` tricks don't close
the widened variable and `Sequence` covariance holes, and widening
`match` arms one by one leaves the annotation lying. So stop lying
instead and spell out what PEP 484 actually admits, at zero runtime
cost.

The docstring example is mirrored as a regular test because the example
linter only extracts module, class and function docstrings, so an
example documenting a module attribute is never checked.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The change is a pure widening with no effect on callers: `int` arguments
were already accepted by type checkers via the numeric tower and already
handled correctly by `math.isclose()`, the annotation just didn't admit
it. Making it explicit means readers no longer have to guess whether
integers are supported.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax
llucax requested a review from a team as a code owner August 13, 2026 11:14
@llucax
llucax requested review from florian-wagner-frequenz and removed request for a team August 13, 2026 11:14
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:typing Affects the typing module part:math Affects the math module labels Aug 13, 2026
@llucax llucax added this to the v1.4.0 milestone Aug 13, 2026
@llucax llucax self-assigned this Aug 13, 2026
@llucax
llucax requested a review from Marenz August 13, 2026 11:15
@llucax
llucax enabled auto-merge August 13, 2026 11:15
@llucax

llucax commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

I will make a v1.4.0 release after this is merged.

@llucax llucax removed this from the v1.4.0 milestone Aug 13, 2026
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 part:typing Affects the typing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a FloatInt (float | int) type alias

1 participant