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

Raise TypeError for order comparisons of a UnitScalar with a float #86

Open
mdickinson opened this issue Apr 18, 2019 · 0 comments
Open

Comments

@mdickinson
Copy link
Member

Currently an order comparison (e.g., <) between a UnitScalar and a float returns a result based on the value for the UnitScalar. That comparison doesn't really quite make sense except in the case where the UnitScalar is actually unitless, and we end up with odd-looking situations like the following, where < doesn't give a partial order:

>>> x = UnitScalar(2.0, units="ft")
>>> y = UnitScalar(1.0, units="m")
>>> z = 1.5
>>> x < y
UnitScalar(True, units='None')
>>> y < z
UnitScalar(True, units='None')
>>> z < x
UnitScalar(True, units='None')

I'd suggest that the last two comparisons above should raise TypeError.

For comparison, we do get an exception (though not TypeError) when comparing incomparable UnitScalar objects:

Python 3.6.0 |Enthought, Inc. (x86_64)| (default, Mar  3 2017, 03:26:01) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from scimath.units.api import UnitScalar
>>> x = UnitScalar(3.2, units="kg")
>>> y = UnitScalar(2.3, units="m")
>>> x < y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/.edm/envs/test36/lib/python3.6/site-packages/scimath/units/unit_array.py", line 415, in __lt__
    other, u = self.__convert_other(other)
  File "/Users/mdickinson/.edm/envs/test36/lib/python3.6/site-packages/scimath/units/unit_array.py", line 247, in __convert_other
    other = convert(numpy.array(other), ou, su)
  File "/Users/mdickinson/.edm/envs/test36/lib/python3.6/site-packages/scimath/units/convert.py", line 102, in convert
    raise ex
  File "/Users/mdickinson/.edm/envs/test36/lib/python3.6/site-packages/scimath/units/convert.py", line 97, in convert
    factor = float(from_unit / to_unit)
  File "/Users/mdickinson/.edm/envs/test36/lib/python3.6/site-packages/scimath/units/unit.py", line 149, in __float__
    raise InvalidConversion(self)
scimath.units.unit.InvalidConversion: dimensional quantities ('m*kg**-1') cannot be converted to scalars
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

No branches or pull requests

1 participant