# Buggy code
def add_field(self, name, function=None, sampling_type=None, **kwargs):
    if not isinstance(name, tuple):
        if kwargs.setdefault("particle_type", False):
            name = ("all", name)
        else:
            name = ("gas", name)
    override = kwargs.get("force_override", False)
    # Handle the case where the field has already been added.
    if not override and name in self:
        mylog.error(
            "Field %s already exists. To override use " + "force_override=True.",
            name,
        )
    if kwargs.setdefault("particle_type", False):
        if sampling_type is not None and sampling_type != "particle":
            raise RuntimeError(
                "Clashing definition of 'sampling_type' and "
                "'particle_type'. Note that 'particle_type' is "
                "deprecated. Please just use 'sampling_type'."
            )
        else:
            sampling_type = "particle"
    if sampling_type is None:
        warnings.warn(
            "Because 'sampling_type' is not specified, yt will "
            "assume a 'cell' sampling_type for the %s field" % (name,),
            stacklevel=3,
        )
        sampling_type = "cell"
    return super(LocalFieldInfoContainer, self).add_field(
        name, sampling_type, function, **kwargs
    )


# 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 add_field(self, name, function, sampling_type, **kwargs):
    sampling_type = self._sanitize_sampling_type(
        sampling_type, kwargs.get("particle_type")
    )

    if isinstance(name, str) or not iterable(name):
        if sampling_type == "particle":
            ftype = "all"
        else:
            ftype = "gas"
        name = (ftype, name)

    override = kwargs.get("force_override", False)
    # Handle the case where the field has already been added.
    if not override and name in self:
        mylog.warning(
            "Field %s already exists. To override use `force_override=True`.",
            name,
        )

    return super(LocalFieldInfoContainer, self).add_field(
        name, function, sampling_type, **kwargs
    )