Skip to content

Conversation

@llucax
Copy link
Contributor

@llucax llucax commented Feb 13, 2024

This is only applying a factor so it should be safe for any Quantity.

This PR also improve multiplication tests to use hypothesis and test with all constructors.

@llucax llucax requested a review from a team as a code owner February 13, 2024 09:52
@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 13, 2024
@llucax llucax force-pushed the quantity-mul-float branch from 1602271 to 66375c2 Compare February 13, 2024 09:54
@llucax llucax self-assigned this Feb 13, 2024
@llucax llucax added this to the v1.0.0-rc5 milestone Feb 13, 2024
@llucax
Copy link
Contributor Author

llucax commented Feb 13, 2024

@llucax llucax added the type:enhancement New feature or enhancement visitble to users label Feb 13, 2024
@llucax llucax marked this pull request as draft February 13, 2024 12:50
This is only applying a factor so it should be safe for any Quantity.

To be able to use nicer names in overloads, we use positional-only
arguments for `__mul__()`, which in practice shouldn't be much of a
breaking change as these methods should be used mostly via operators.

We also use `match` syntax in the changed methods.

Signed-off-by: Leandro Lucarella <[email protected]>
These methods have a generic `type: ignore` with no clarification why.

The issue is we need the ignore here because otherwise `mypy` will give
this error:

> Overloaded operator methods can't have wider argument types in
> overrides

The problem seems to be when the other type implements an
**incompatible** __rmul__ method, which is not the case here, so we
should be safe.

Please see this example:
https://github.com/python/mypy/blob/c26f1297d4f19d2d1124a30efc97caebb8c28616/test-data/unit/check-overloading.test#L4738C1-L4769C55

And a discussion in a mypy issue here:
python/mypy#4985 (comment)

For more information.

This commit uses a more specific `type: ignore[override]` and add a
comment clarifying this.

Signed-off-by: Leandro Lucarella <[email protected]>
We make the tests more generic, testing with all constructors, and use
hypothesis to have a better (and more random) coverage.

Signed-off-by: Leandro Lucarella <[email protected]>
Signed-off-by: Leandro Lucarella <[email protected]>
Signed-off-by: Leandro Lucarella <[email protected]>
@llucax llucax force-pushed the quantity-mul-float branch from 66375c2 to 5118985 Compare February 14, 2024 10:38
@llucax llucax marked this pull request as ready for review February 14, 2024 10:38
@llucax
Copy link
Contributor Author

llucax commented Feb 14, 2024

Reabsed on top of the merged #874, this is ready for review @daniel-zullo-frequenz /

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.

Review done, only found a few cosmetics in the docs. LGTM otherwise

Co-authored-by: daniel-zullo-frequenz <[email protected]>
Signed-off-by: Leandro Lucarella <[email protected]>
@llucax
Copy link
Contributor Author

llucax commented Feb 14, 2024

Thanks @daniel-zullo-frequenz, I applied all your suggestions. Since it affects different commits, I'd rather leave the applied suggestions as a separate commit to avoid too much rabasing and amending.

@llucax llucax added this pull request to the merge queue Feb 15, 2024
Merged via the queue into frequenz-floss:v1.x.x with commit 8aa7977 Feb 15, 2024
@llucax llucax deleted the quantity-mul-float branch February 15, 2024 11:02
# https://github.com/python/mypy/blob/c26f1297d4f19d2d1124a30efc97caebb8c28616/test-data/unit/check-overloading.test#L4738C1-L4769C55
# And a discussion in a mypy issue here:
# https://github.com/python/mypy/issues/4985#issuecomment-389692396
@overload # type: ignore[override]
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to add some more context, when Marenz was adding these, we had no idea what the issue was, because without these, mypy kept crashing. Looks like they've fixed that bug and now we are able to understand the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I didn't get a crash (thankfully!) but I still had to do my research, because the error message is not very clear why is there a problem with doing that.


quantity *= percentage
print(f"*{quantity=}")
assert quantity.base_value == expected_value
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to retain the print statements? don't they pollute the test output when trying to debug something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are useful when something fails. I guess if you are focusing on some other test you can always run pytest test_other.py::my_interesting_test if you want to keep the noise down. If someone wants to remove it in the future, I'm also fine with that, as if one gets a failure is not that hard to add the print() (except maybe if it is only happening in the CI).

Copy link
Contributor

Choose a reason for hiding this comment

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

Well there's always:

assert quantity.base_value == expected_value, f"{quantity=} * {percentage=} != {expected_value}! 🙀 "

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 type:enhancement New feature or enhancement visitble to users

Projects

Development

Successfully merging this pull request may close these issues.

3 participants