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

Add correct comparison support for Scalar #31

Closed

Conversation

arthursoprana
Copy link
Contributor

@arthursoprana arthursoprana commented Oct 4, 2019

This PR attempts to fix <= and >= comparisons with Scalar

Fix #26

@codecov
Copy link

codecov bot commented Oct 4, 2019

Codecov Report

Merging #31 into master will increase coverage by 0.08%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
+ Coverage   97.47%   97.55%   +0.08%     
==========================================
  Files          53       53              
  Lines        9270     9339      +69     
==========================================
+ Hits         9036     9111      +75     
+ Misses        234      228       -6

@arthursoprana arthursoprana force-pushed the fb-BARRIL-26-fix-scalar-compare branch from 954f5c6 to 11e62f0 Compare October 4, 2019 20:12
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Please add a CHANGELOG entry

src/barril/units/_quantity.py Outdated Show resolved Hide resolved
@arthursoprana arthursoprana force-pushed the fb-BARRIL-26-fix-scalar-compare branch from 1fcda5a to 6479e66 Compare October 7, 2019 20:20
@@ -242,8 +242,8 @@ def GetFormatted(self, unit=None, value_format=None):
def __eq__(self, other):
return (
type(self) is type(other)
and self._value == other.value
and self._quantity == other._quantity
and self._value == other.GetValue(self.unit)
Copy link
Member

Choose a reason for hiding this comment

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

Due to floating point rounding self._value == other.GetValue(self.unit) this could lead to situations where (a == b) != (b == a).
I think this should agree on the unit used for comparison. Maybe the one composed by composing quantities base units.

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've refactored to do the comparison always using DefaultUnit:

    def __eq__(self, other):
        return (
            type(self) is type(other)
            and self._quantity._category == other._quantity._category
            and self._GetValueInDefaultUnit() == other._GetValueInDefaultUnit()
        )

    def _GetValueInDefaultUnit(self):
        try:
            return self.GetValue(
                self._unit_database.GetDefaultUnit(self._quantity._category)
            )
        except InvalidQuantityTypeError:
            return self._value

Copy link
Member

@prusse-martin prusse-martin Oct 11, 2019

Choose a reason for hiding this comment

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

Scalars with the same category could have different units...
I don't think returning the raw value is a good option in InvalidQuantityTypeError.

Maybe return (self._value, self._quantity._unit)? If you do something like this I recommend a docstring stating that.

I just noted that this is also used on almost equal. Sorry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, ok. I've took you suggestion, but with a slightly modification, take a look.

src/barril/units/_scalar.py Outdated Show resolved Hide resolved
@arthursoprana arthursoprana force-pushed the fb-BARRIL-26-fix-scalar-compare branch 4 times, most recently from 8638425 to 7dbe280 Compare October 10, 2019 16:37
@arthursoprana arthursoprana force-pushed the fb-BARRIL-26-fix-scalar-compare branch 2 times, most recently from 28c6e14 to e4657c5 Compare October 10, 2019 21:23
Modified comparison behavior of ``Scalar``. The previous behavior assumes that ``Scalar(1, "m") != Scalar(100, "cm")`` and not it has been changed to ``Scalar(1, "m") == Scalar(100, "cm")``. This may affect users that rely on the previous behavior.

BARRIL-26
@arthursoprana
Copy link
Contributor Author

There are codebases that rely on the current comparison behavior. Decided not to merge it at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scalars should compare equal if the underlying quantity measure is OK
4 participants