Add a FloatInt (float | int) type alias - #189
Open
llucax wants to merge 3 commits into
Open
Conversation
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
requested review from
florian-wagner-frequenz
and removed request for
a team
August 13, 2026 11:14
llucax
enabled auto-merge
August 13, 2026 11:15
Contributor
Author
|
I will make a v1.4.0 release after this is merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a port of the
FloatInttype 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.