facebookresearch/detectron2 · error · AttributeError

Class with @configurable must have a 'from_config' classmeth

Error message

Class with @configurable must have a 'from_config' classmethod.

What it means

The @configurable decorator requires the class to define a from_config classmethod so it can translate aCfg/config into __init__ kwargs. Accessing type(self).from_config raised AttributeError, meaning the class (or a subclass) lacks the method entirely.

Source

Thrown at detectron2/config/config.py:182

            class must have a ``from_config`` classmethod which takes `cfg` as
            the first argument.
        from_config (callable): the from_config function in usage 2. It must take `cfg`
            as its first argument.
    """

    if init_func is not None:
        assert (
            inspect.isfunction(init_func)
            and from_config is None
            and init_func.__name__ == "__init__"
        ), "Incorrect use of @configurable. Check API documentation for examples."

        @functools.wraps(init_func)
        def wrapped(self, *args, **kwargs):
            try:
                from_config_func = type(self).from_config
            except AttributeError as e:
                raise AttributeError(
                    "Class with @configurable must have a 'from_config' classmethod."
                ) from e
            if not inspect.ismethod(from_config_func):
                raise TypeError("Class with @configurable must have a 'from_config' classmethod.")

            if _called_with_cfg(*args, **kwargs):
                explicit_args = _get_args_from_config(from_config_func, *args, **kwargs)
                init_func(self, **explicit_args)
            else:
                init_func(self, *args, **kwargs)

        return wrapped

    else:
        if from_config is None:
            return configurable  # @configurable() is made equivalent to @configurable
        assert inspect.isfunction(
            from_config

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Add a from_config classmethod: @classmethod def from_config(cls, cfg): return {'arg': cfg.MODEL.MY_ARG, ...}
  2. Ensure from_config takes 'cfg' as its first parameter (after cls)
  3. Check that no intermediate class in the MRO deletes or misnames from_config

Example fix

# before
@configurable
def __init__(self, in_ch, out_ch): ...
# after
@classmethod
def from_config(cls, cfg):
    return {"in_ch": cfg.MODEL.IN_CH, "out_ch": cfg.MODEL.OUT_CH}
@configurable
def __init__(self, in_ch, out_ch): ...
Defensive patterns

Strategy: type-guard

Validate before calling

assert hasattr(MyClass, "from_config") and inspect.ismethod(MyClass.from_config), "class must define from_config classmethod"

Type guard

def is_configurable_ok(cls) -> bool:
    return callable(getattr(cls, "from_config", None)) and inspect.ismethod(cls.from_config)

Try / catch

try:
    obj = MyClass(cfg)
except (AttributeError, TypeError) as e:
    if "from_config" in str(e):
        obj = MyClass(**manual_kwargs)  # bypass with explicit kwargs
    else:
        raise

Prevention

When it happens

Trigger: Decorating a class's __init__ with @configurable without defining a matching from_config(cls, cfg) classmethod; or instantiating a @configurable class whose subclass overrode/removed from_config.

Common situations: Adding @configurable to a custom backbone/head but forgetting the from_config boilerplate; refactors that rename from_config; inheriting from a configurable class while shadowing it incorrectly.

Related errors


AI-assisted analysis of facebookresearch/detectron2@a2f4a8771a (2026-08-27). Data as JSON: /api/errors/61a0f948bfb9c6c2. Report an issue: GitHub.