# Buggy code
def _create_unit_registry(self, unit_system):
    from yt.units import dimensions as dimensions

    # yt assumes a CGS unit system by default (for back compat reasons).
    # Since unyt is MKS by default we specify the MKS values of the base
    # units in the CGS system. So, for length, 1 cm = .01 m. And so on.
    self.unit_registry = UnitRegistry(unit_system=unit_system)
    self.unit_registry.add("code_length", 0.01, dimensions.length)
    self.unit_registry.add("code_mass", 0.001, dimensions.mass)
    self.unit_registry.add("code_density", 1000.0, dimensions.density)
    self.unit_registry.add(
        "code_specific_energy", 1.0, dimensions.energy / dimensions.mass
    )
    self.unit_registry.add("code_time", 1.0, dimensions.time)
    if unit_system == "mks":
        self.unit_registry.add("code_magnetic", 1.0, dimensions.magnetic_field)
    else:
        self.unit_registry.add("code_magnetic", 0.1**0.5, dimensions.magnetic_field_cgs)
    self.unit_registry.add("code_temperature", 1.0, dimensions.temperature)
    self.unit_registry.add("code_pressure", 0.1, dimensions.pressure)
    self.unit_registry.add("code_velocity", 0.01, dimensions.velocity)
    self.unit_registry.add("code_metallicity", 1.0, dimensions.dimensionless)
    self.unit_registry.add("h", 1.0, dimensions.dimensionless, r"h")
    self.unit_registry.add("a", 1.0, dimensions.dimensionless)


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-01d8197cb9a9> in <module>
6     return data["x"].base * unyt.msun
7 ds = yt.testing.fake_amr_ds(fields=["density"])
----> 8 ds.add_field(name="test", func=_test, sampling_type="cell", units="kg")

~/dev/python/yt-project/yt/yt/data_objects/static_output.py in add_field(self, name, function, sampling_type, **kwargs)
1271         self.field_info.add_field(name, sampling_type, function=function, **kwargs)
1272         self.field_info._show_field_errors.append(name)
-> 1273         deps, _ = self.field_info.check_derived_fields([name])
1274         self.field_dependencies.update(deps)
1275

~/dev/python/yt-project/yt/yt/fields/field_info_container.py in check_derived_fields(self, fields_to_check)
395         for field in fields_to_check:
396             if field not in self:
--> 397                 raise YTFieldNotFound(str(field))
398             fi = self[field]
399             try:

TypeError: __init__() missing 1 required positional argument: 'ds'

# Fixed code
def _create_unit_registry(self, unit_system):
    # yt assumes a CGS unit system by default (for back compat reasons).
    # Since unyt is MKS by default we specify the MKS values of the base
    # units in the CGS system. So, for length, 1 cm = .01 m. And so on.
    self.unit_registry = UnitRegistry(unit_system=unit_system)
    self.unit_registry.add("code_length", 0.01, dimensions.length)
    self.unit_registry.add("code_mass", 0.001, dimensions.mass)
    self.unit_registry.add("code_density", 1000.0, dimensions.density)
    self.unit_registry.add(
        "code_specific_energy", 1.0, dimensions.energy / dimensions.mass
    )
    self.unit_registry.add("code_time", 1.0, dimensions.time)
    if unit_system == "mks":
        self.unit_registry.add("code_magnetic", 1.0, dimensions.magnetic_field)
    else:
        self.unit_registry.add("code_magnetic", 0.1**0.5, dimensions.magnetic_field_cgs)
    self.unit_registry.add("code_temperature", 1.0, dimensions.temperature)
    self.unit_registry.add("code_pressure", 0.1, dimensions.pressure)
    self.unit_registry.add("code_velocity", 0.01, dimensions.velocity)
    self.unit_registry.add("code_metallicity", 1.0, dimensions.dimensionless)
    self.unit_registry.add("h", 1.0, dimensions.dimensionless, r"h")
    self.unit_registry.add("a", 1.0, dimensions.dimensionless)