Skip to content

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

@mdickinson

Description

@mdickinson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions