Raise TypeError for order comparisons of a `UnitScalar` with a float
- Dominant language
- Python
- Stars
- 66
- Forks
- 13
- PR merge metrics
- No merged PRs in 30d
Description
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:
```python
>>> 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 "", line 1, in
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
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.