-
Notifications
You must be signed in to change notification settings - Fork 17
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
Conversation
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>
37ae784
to
8a045c2
Compare
There was a problem hiding this 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
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>
8a045c2
to
674a534
Compare
Updated to address the comments. |
There was a problem hiding this 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
b2459ce
to
563c63b
Compare
Haha, you can clearly see I wrote that in a rush :P Updated. |
Enabled auto-merge. |
563c63b
to
af027ef
Compare
This comment was marked as outdated.
This comment was marked as outdated.
8779dec
to
af027ef
Compare
This comment was marked as outdated.
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>
af027ef
to
15c0d14
Compare
This comment was marked as outdated.
This comment was marked as outdated.
# 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 |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 = [ |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
In Python
float
is a bit weird, becauseisinstance(1, float)
isFalse
but stillint
is a subtype offloat
in terms of the type system, soint
will be accepted when afloat
is specified in arguments for example.This leads to quantities that can have
int
as base value if it was constructed with aint
instead of afloat
, which is unintuitive when thebase_value
type hint isfloat
and can potentially cause problems, in particular when addressing #821.