Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Quantity comparisons less error prone #842

Open
llucax opened this issue Jan 15, 2024 · 0 comments
Open

Make Quantity comparisons less error prone #842

llucax opened this issue Jan 15, 2024 · 0 comments
Labels
part:core Affects the SDK core components (data structures, etc.) type:enhancement New feature or enhancement visitble to users
Milestone

Comments

@llucax
Copy link
Contributor

llucax commented Jan 15, 2024

What's needed?

We need to make it as hard as possible for users to shot themselves in the foot.

Allowing == comparisons with Quantitys is generally unsafe, because it uses a float underneath.

Proposed solution

Disallow using == with Quantitys.

Ideally it should be rejected by the type system (mypy), but it should also raise an exception (or return NotImplemented if type checking is not used). If possible users should be redirected to using Quantity.isclose() instead.

Ideally isclose() should probably handle 0 (or values close to 0) specially, as documented in math.isclose.

Users can always escape this by accessing the raw float and using == with that in weird cases where == might be necessary, but that should be considered a low-level operation.

Use cases

This came up when trying to see if a battery pool is full or empty. Naturally one would do soc == Percentage(100), but this could have issues if the soc calculation has some precision issue and it is something like 99.999999 instead. But this problem is much more general than that, users should always try to think in terms of isclose() instead of strict equality with floats.

@llucax llucax added type:enhancement New feature or enhancement visitble to users part:core Affects the SDK core components (data structures, etc.) labels Jan 15, 2024
@llucax llucax added this to the v1.0.0-rc4 milestone Jan 15, 2024
@llucax llucax modified the milestones: v1.0.0-rc4, v1.0.0-rc5, v1.0.0-rc6 Feb 1, 2024
@llucax llucax modified the milestones: v1.0.0-rc6, v1.0.0-rc7 Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:core Affects the SDK core components (data structures, etc.) type:enhancement New feature or enhancement visitble to users
Projects
Status: To do
Development

No branches or pull requests

1 participant