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/docs/usage.rst b/docs/usage.rst
index 0af413e..20b28c6 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -1023,7 +1023,7 @@ In practice, the unit metadata for a unit object is contained in an instance of
 
   >>> from unyt import g
   >>> g.registry  # doctest: +ELLIPSIS
-  <unyt.unit_registry.UnitRegistry ...>
+  <unyt.unit_registry._NonModifiableUnitRegistry ...>
 
 All the unit objects in the :mod:`unyt` namespace make use of the default unit
 registry, importable as :data:`unyt.unit_registry.default_unit_registry`. This
diff --git a/unyt/unit_registry.py b/unyt/unit_registry.py
index 5dfeed3..755f4bc 100644
--- a/unyt/unit_registry.py
+++ b/unyt/unit_registry.py
@@ -266,8 +266,18 @@ class UnitRegistry:
         return type(self)(lut=lut)
 
 
+class _NonModifiableUnitRegistry(UnitRegistry):
+    """The class of the default unit registry"""
+
+    def modify(self, symbol, base_value):
+        raise TypeError("Units from unyt's default registry cannot be modified.")
+
+    def remove(self, symbol):
+        raise TypeError("Units from unyt's default registry cannot be removed.")
+
+
 #: The default unit registry
-default_unit_registry = UnitRegistry()
+default_unit_registry = _NonModifiableUnitRegistry()
 
 
 def _lookup_unit_symbol(symbol_str, unit_symbol_lut):


Test patch:
diff --git a/unyt/tests/test_unit_registry.py b/unyt/tests/test_unit_registry.py
index 532389e..0b599c3 100644
--- a/unyt/tests/test_unit_registry.py
+++ b/unyt/tests/test_unit_registry.py
@@ -45,6 +45,18 @@ def test_add_modify_error():
     assert ureg["tayne"][:3] == ureg["m"][:3]
 
 
+def test_modify_symbol_from_default_unit_registry():
+    # see https://github.com/yt-project/unyt/issues/473
+    from unyt import km
+    from unyt.unit_object import default_unit_registry
+
+    with pytest.raises(TypeError):
+        default_unit_registry.modify("cm", 10 * km)
+
+    with pytest.raises(TypeError):
+        default_unit_registry.remove("cm")
+
+
 def test_keys():
     ureg = UnitRegistry()
     assert sorted(ureg.keys()) == sorted(ureg.lut.keys())