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 sure base_value is always float #874

Merged
merged 4 commits into from
Feb 13, 2024

Conversation

llucax
Copy link
Contributor

@llucax llucax commented Feb 9, 2024

In Python float is a bit weird, because isinstance(1, float) is False but still int is a subtype of float in terms of the type system, so int will be accepted when a float is specified in arguments for example.

This leads to quantities that can have int as base value if it was constructed with a int instead of a float, which is unintuitive when the base_value type hint is float and can potentially cause problems, in particular when addressing #821.

@llucax llucax requested a review from a team as a code owner February 9, 2024 13:06
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:data-pipeline Affects the data pipeline labels Feb 9, 2024
@llucax llucax self-assigned this Feb 9, 2024
@llucax llucax added this to the v1.0.0-rc5 milestone Feb 9, 2024
Mainly for DRY, but also to have a central point to fix bugs, like
`base_value` not being always `float`.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Copy link
Contributor

@daniel-zullo-frequenz daniel-zullo-frequenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a couple of minor comments to check for, LGTM otherwise

tests/timeseries/test_quantities.py Outdated Show resolved Hide resolved
tests/timeseries/test_quantities.py Outdated Show resolved Hide resolved
In Python `float` is a bit weird, because `isinstance(1, float)` is
`False` but still `int` is a subtype of `float` in terms of the type
system, so `int` will be accepted when a `float` is specified in
arguments for example.

This leads to quantities that can have `int` as base value if it was
constructed with a `int` instead of a `float`, which is unintuitive
when the `base_value` type hint is `float` and can potentially cause
problems.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax
Copy link
Contributor Author

llucax commented Feb 13, 2024

Updated to address the comments.

Copy link
Contributor

@daniel-zullo-frequenz daniel-zullo-frequenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few typos and LGTM

@llucax
Copy link
Contributor Author

llucax commented Feb 13, 2024

Haha, you can clearly see I wrote that in a rush :P

Updated.

@llucax
Copy link
Contributor Author

llucax commented Feb 13, 2024

Enabled auto-merge.

@llucax llucax enabled auto-merge February 13, 2024 13:37
@llucax

This comment was marked as outdated.

@llucax llucax force-pushed the fix-quantity-zero branch 2 times, most recently from 8779dec to af027ef Compare February 13, 2024 13:42
@llucax

This comment was marked as outdated.

`test_to_and_from_string()` is **extremely** fragile, even the most
innocent change can break it, so we add some extra comments to make sure
other people don't attempt to change it and avoid them some headaches.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Co-authored-by: daniel-zullo-frequenz <120166726+daniel-zullo-frequenz@users.noreply.github.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax

This comment was marked as outdated.

@llucax llucax added this pull request to the merge queue Feb 13, 2024
Merged via the queue into frequenz-floss:v1.x.x with commit 5df1db5 Feb 13, 2024
14 checks passed
@llucax llucax deleted the fix-quantity-zero branch February 13, 2024 14:21
# an empty list. With this we should at least make sure we are not testing less classes
# than before. We don't get the actual number using len(_QUANTITY_SUBCLASSES) because it
# would defeat the purpose of the test.
_SANITFY_NUM_CLASSES = 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit late to the review, but it might be better to call this _SANITIFY_NUM_CLASSES.. I just don't see a reason to skip this extra I...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just try to pronounce it with and without :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree completely. When making typos, please make sure you don't skimp on the vowels. :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how I ended up typing this. But luckily we have smart completion, so typos are properly propagated and the code never fails!

)
]

# A very basic sanity check that are messing up the introspection
Copy link
Contributor

@Marenz Marenz Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# A very basic sanity check that are messing up the introspection

Someone def. messed up this comment :)

for unit in _QUANTITY_BASE_UNIT_STRINGS:
assert unit is not None

_QUANTITY_CTORS = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So close, we could have called these QTY_CTORS and pronounced them the "cutty cutters"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I made my first meme-PR!

# an empty list. With this we should at least make sure we are not testing less classes
# than before. We don't get the actual number using len(_QUANTITY_SUBCLASSES) because it
# would defeat the purpose of the test.
_SANITFY_NUM_CLASSES = 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree completely. When making typos, please make sure you don't skimp on the vowels. :D

# )
# But we can't do that now, because, you guessed it, it will also break the tests
# (_new() will use 10.0**exponent instead of 10**exponent, which seems to have some
# effect on the tests.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By "on the tests", I guess you mean just this test, because hypothesis also generates some int inputs when we give it a float range?

This function seems to just compare the value from quantity_type.from_string with the value from quantity._base_value. If they don't match up, maybe the from_string method just needs to convert to float as well?

And then they can be compared with quantity.is_close rather than a string comparison?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests
Projects
Development

Successfully merging this pull request may close these issues.

4 participants