Repository: hgrecco/pint

Issue:
`is_compatible_with` ignores active contexts
While working on #1421 I noticed that `is_compatible_with` allows working with contexts, but they have to be passed explicitly: contexts that were activated using `ureg.context` (both when used as a function and as a context manager) will be ignored:
```python
In [6]: ureg = pint.UnitRegistry()
   ...: 
   ...: a = 532.0 * ureg.nm
   ...: b = 563.5 * ureg.terahertz
   ...: 
   ...: with ureg.context("sp"):
   ...:     print(a.to(ureg.terahertz))
   ...:     assert a.is_compatible_with(b), "not compatible"
   ...: 
563.5196578947367 terahertz
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-8eaa63cfa960> in <module>
      6 with ureg.context("sp"):
      7     print(a.to(ureg.terahertz))
----> 8     assert a.is_compatible_with(b), "not compatible"
      9 

AssertionError: not compatible
```

(see also https://github.com/hgrecco/pint/pull/1421#issuecomment-978335230)

Discussion:
keewis: wow, that was an easy fix. Thanks! It might be good to add tests, though?
jules-ch: I forgot to push them, I'll do that tomorrow

Patch:
diff --git a/CHANGES b/CHANGES
index ac7bfe2..7405124 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,7 @@ Pint Changelog
 
 - Upgrade min version of uncertainties to 3.1.4
 - Fix setting options of the application registry (Issue #1403).
+- Fix Quantity & Unit `is_compatible_with` with registry active contexts (Issue #1424).
 
 
 0.18 (2021-10-26)
diff --git a/pint/quantity.py b/pint/quantity.py
index d70b65c..f11c790 100644
--- a/pint/quantity.py
+++ b/pint/quantity.py
@@ -648,7 +648,7 @@ class Quantity(PrettyIPython, SharedRegistryObject, Generic[_MagnitudeType]):
         -------
         bool
         """
-        if contexts:
+        if contexts or self._REGISTRY._active_ctx:
             try:
                 self.to(other, *contexts, **ctx_kwargs)
                 return True
diff --git a/pint/unit.py b/pint/unit.py
index 8651900..367c7fd 100644
--- a/pint/unit.py
+++ b/pint/unit.py
@@ -168,7 +168,7 @@ class Unit(PrettyIPython, SharedRegistryObject):
         -------
         bool
         """
-        if contexts:
+        if contexts or self._REGISTRY._active_ctx:
             try:
                 (1 * self).to(other, *contexts, **ctx_kwargs)
                 return True


Test patch:
diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py
index 554771a..ddb242d 100644
--- a/pint/testsuite/test_quantity.py
+++ b/pint/testsuite/test_quantity.py
@@ -596,6 +596,27 @@ class TestQuantity(QuantityTestCase):
             self.Q_(FakeWrapper(42), "m")
         assert FakeWrapper(self.Q_(42, "m")).q == self.Q_(42, "m")
 
+    def test_is_compatible_with(self):
+        a = self.Q_(1, "kg")
+        b = self.Q_(20, "g")
+        c = self.Q_(550)
+
+        assert a.is_compatible_with(b)
+        assert a.is_compatible_with("lb")
+        assert a.is_compatible_with(self.U_("lb"))
+        assert not a.is_compatible_with("km")
+        assert not a.is_compatible_with("")
+        assert not a.is_compatible_with(12)
+
+        assert c.is_compatible_with(12)
+
+    def test_is_compatible_with_with_context(self):
+        a = self.Q_(532.0, "nm")
+        b = self.Q_(563.5, "terahertz")
+        assert a.is_compatible_with(b, "sp")
+        with self.ureg.context("sp"):
+            assert a.is_compatible_with(b)
+
 
 class TestQuantityToCompact(QuantityTestCase):
     def assertQuantityAlmostIdentical(self, q1, q2):