Repository: yt-project/unyt

Issue:
unable to use custom unit_registry nor modify base_value for unit in default_unit_registry
* unyt version: 2.9.5
* Python version: 3.10.1
* Operating System: macOS Ventura 13.2

### Description

I have two issues. The first is when creating a custom unit_registry I can't seem to be able to be able to push the changes in any meaningful way to use the units in said registry. The second is when adding custom units to the default unit registry, I can't modify the base_value after the fact.

### What I Did

```
import unyt

# ISSUE 1
my_registry = unyt.UnitRegistry()
unyt.unit_object.define_unit('bushel', 0.5*unyt.kg, registry=my_registry)
a = 2. * unyt.bushel

Traceback (most recent call last):
  File "/Users/adamcohan/Desktop/Clinic/pygreet/testing_units/testing_registries.py", line 5, in <module>
    a = 2. * unyt.bushel
AttributeError: module 'unyt' has no attribute 'bushel'

# ISSUE 2
unyt.unit_object.define_unit('bushel', 0.5*unyt.kg)
print(unyt.bushel.base_value)
print(unyt.unit_registry.default_unit_registry['bushel'])
unyt.unit_registry.default_unit_registry.modify('bushel', 0.1*unyt.kg)
print(unyt.bushel.base_value)
print(unyt.unit_registry.default_unit_registry['bushel'])

0.5
(0.5, (mass), 0.0, '\\rm{bushel}', False)
0.5
(0.1, (mass), 0.0, '\\rm{bushel}', False)
```


Discussion:

Patch:
diff --git a/unyt/unit_registry.py b/unyt/unit_registry.py
index 755f4bc..d400ec3 100644
--- a/unyt/unit_registry.py
+++ b/unyt/unit_registry.py
@@ -185,6 +185,8 @@ class UnitRegistry:
             )
 
         del self.lut[symbol]
+        if symbol in self._unit_object_cache:
+            del self._unit_object_cache[symbol]
 
     def modify(self, symbol, base_value):
         """
@@ -216,6 +218,8 @@ class UnitRegistry:
             new_dimensions = self.lut[symbol][1]
 
         self.lut[symbol] = (float(base_value), new_dimensions) + self.lut[symbol][2:]
+        if symbol in self._unit_object_cache:
+            del self._unit_object_cache[symbol]
 
     def keys(self):
         """


Test patch:
diff --git a/unyt/tests/test_unit_registry.py b/unyt/tests/test_unit_registry.py
index 0b599c3..94fbcca 100644
--- a/unyt/tests/test_unit_registry.py
+++ b/unyt/tests/test_unit_registry.py
@@ -57,6 +57,33 @@ def test_modify_symbol_from_default_unit_registry():
         default_unit_registry.remove("cm")
 
 
+def test_modify_cache_clear():
+    # see https://github.com/yt-project/unyt/issues/473
+
+    ureg = UnitRegistry()
+    ureg.add("celery", 1.0, length)
+    u0 = Unit("m", registry=ureg)
+    u1 = Unit("celery", registry=ureg)
+    assert u1 == u0
+
+    ureg.modify("celery", 0.5)
+
+    # check that this equality still holds post registry modification
+    assert u1 == u0
+
+    u2 = Unit("celery", registry=ureg)
+    assert u2 != u1
+    assert 1.0 * u2 == 0.5 * u0
+
+
+def test_remove_unit():
+    ureg = UnitRegistry()
+    ureg.add("celery", 1.0, length)
+    ureg.remove("celery")
+    with pytest.raises(UnitParseError):
+        Unit("celery", registry=ureg)
+
+
 def test_keys():
     ureg = UnitRegistry()
     assert sorted(ureg.keys()) == sorted(ureg.lut.keys())