summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLewis A. Marshall <lewis@purigenbio.com>2020-10-31 11:00:28 -0700
committerLewis A. Marshall <lewis@purigenbio.com>2020-10-31 11:00:28 -0700
commit79dc97e652d67f4a9eae4b8be6590374a32edd17 (patch)
tree4191253d3d6277de2ae6985d788693b517437b17
parent5e59f373ea237a95f22960a2b169c3a756a264c9 (diff)
downloadpint-79dc97e652d67f4a9eae4b8be6590374a32edd17.tar.gz
Quantity comparisons ensure other is Quantity.
-rw-r--r--pint/quantity.py10
-rw-r--r--pint/testsuite/test_measurement.py8
-rw-r--r--pint/testsuite/test_quantity.py9
3 files changed, 24 insertions, 3 deletions
diff --git a/pint/quantity.py b/pint/quantity.py
index 0395a65..92cb81b 100644
--- a/pint/quantity.py
+++ b/pint/quantity.py
@@ -1577,7 +1577,7 @@ class Quantity(PrettyIPython, SharedRegistryObject):
@check_implemented
def compare(self, other, op):
- if not isinstance(other, self.__class__):
+ if not isinstance(other, Quantity):
if self.dimensionless:
return op(
self._convert_magnitude_not_inplace(self.UnitsContainer()), other
@@ -1598,6 +1598,14 @@ class Quantity(PrettyIPython, SharedRegistryObject):
else:
raise ValueError("Cannot compare Quantity and {}".format(type(other)))
+
+ # Registry equality check based on util.SharedRegistryObject
+ if self._REGISTRY is not other._REGISTRY:
+ mess = "Cannot operate with {} and {} of different registries."
+ raise ValueError(
+ mess.format(self.__class__.__name__, other.__class__.__name__)
+ )
+
if self._units == other._units:
return op(self._magnitude, other._magnitude)
if self.dimensionality != other.dimensionality:
diff --git a/pint/testsuite/test_measurement.py b/pint/testsuite/test_measurement.py
index b5acff0..5ebb33b 100644
--- a/pint/testsuite/test_measurement.py
+++ b/pint/testsuite/test_measurement.py
@@ -86,7 +86,7 @@ class TestMeasurement(QuantityTestCase):
("{:.3uL}", r"\left(0.2000 \pm 0.0100\right)\ \mathrm{second}^{2}"),
("{:.3uH}", "(0.2000 &plusmn; 0.0100) second<sup>2</sup>"),
("{:.3uC}", "(0.2000+/-0.0100) second**2"),
- ("{:.3uLx}", r"\SI{0.2000 +- 0.0100}{\second\squared}",),
+ ("{:.3uLx}", r"\SI{0.2000 +- 0.0100}{\second\squared}"),
("{:.1uLx}", r"\SI{0.20 +- 0.01}{\second\squared}"),
):
with self.subTest(spec):
@@ -236,3 +236,9 @@ class TestMeasurement(QuantityTestCase):
r.value.magnitude, ml.value.magnitude / mr.value.magnitude
)
self.assertEqual(r.value.units, ml.value.units / mr.value.units)
+
+ def test_measurement_comparison(self):
+ x = self.Q_(4.2, "meter")
+ y = self.Q_(5.0, "meter").plus_minus(0.1)
+ self.assertTrue(x<=y)
+ self.assertFalse(x>=y)
diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py
index b5780e5..fe86801 100644
--- a/pint/testsuite/test_quantity.py
+++ b/pint/testsuite/test_quantity.py
@@ -6,7 +6,7 @@ import pickle
import warnings
from unittest.mock import patch
-from pint import DimensionalityError, OffsetUnitCalculusError, UnitRegistry
+from pint import DimensionalityError, OffsetUnitCalculusError, UnitRegistry, Quantity, get_application_registry
from pint.compat import np
from pint.testsuite import QuantityTestCase, helpers
from pint.testsuite.parameterized import ParameterizedTestCase
@@ -63,6 +63,8 @@ class TestQuantity(QuantityTestCase):
y = self.Q_(4.2, "meter")
z = self.Q_(5, "meter")
j = self.Q_(5, "meter*meter")
+ k = 5 * get_application_registry().meter # Include a comparison to the application registry
+ l = Quantity(5, "meter") # Include a comparison to a directly created Quantity
# identity for single object
self.assertTrue(x == x)
@@ -81,6 +83,11 @@ class TestQuantity(QuantityTestCase):
self.assertTrue(x != z)
self.assertTrue(x < z)
+ # Compare with items to the separate application registry
+ self.assertTrue(k >= l) # These should both be from application registry
+ with self.assertRaises(ValueError):
+ z>l # One from local registry, one from application registry
+
self.assertTrue(z != j)
self.assertNotEqual(z, j)