facebookresearch/detectron2 · error · TypeError

{name} must take 'cfg' as the first argument!

Error message

{name} must take 'cfg' as the first argument!

What it means

When a @configurable class is initialized with a CfgNode/Detectron2 config, _get_args_from_config inspects from_config's signature and requires its first parameter to be named exactly 'cfg'. Any other first parameter name raises this TypeError.

Source

Thrown at detectron2/config/config.py:231

            return wrapped

        return wrapper


def _get_args_from_config(from_config_func, *args, **kwargs):
    """
    Use `from_config` to obtain explicit arguments.

    Returns:
        dict: arguments to be used for cls.__init__
    """
    signature = inspect.signature(from_config_func)
    if list(signature.parameters.keys())[0] != "cfg":
        if inspect.isfunction(from_config_func):
            name = from_config_func.__name__
        else:
            name = f"{from_config_func.__self__}.from_config"
        raise TypeError(f"{name} must take 'cfg' as the first argument!")
    support_var_arg = any(
        param.kind in [param.VAR_POSITIONAL, param.VAR_KEYWORD]
        for param in signature.parameters.values()
    )
    if support_var_arg:  # forward all arguments to from_config, if from_config accepts them
        ret = from_config_func(*args, **kwargs)
    else:
        # forward supported arguments to from_config
        supported_arg_names = set(signature.parameters.keys())
        extra_kwargs = {}
        for name in list(kwargs.keys()):
            if name not in supported_arg_names:
                extra_kwargs[name] = kwargs.pop(name)
        ret = from_config_func(*args, **kwargs)
        # forward the other arguments to __init__
        ret.update(extra_kwargs)
    return ret

View on GitHub (pinned to a2f4a8771a)

Solutions

  1. Rename the first parameter (after cls) of from_config to exactly 'cfg'
  2. Keep from_config(cls, cfg, **kwargs) if you need to forward extra overrides
  3. Re-run after fixing; the check is purely on the parameter name at call time

Example fix

# before
def from_config(cls, d2_cfg): return {'x': d2_cfg.MODEL.X}
# after
def from_config(cls, cfg): return {'x': cfg.MODEL.X}
Defensive patterns

Strategy: validation

Validate before calling

params = list(inspect.signature(MyClass.from_config).parameters)
assert params[0] == "cfg", f"first param must be 'cfg', got {params[0]}"

Type guard

def from_config_signature_ok(cls) -> bool:
    ps = list(inspect.signature(cls.from_config).parameters)
    return bool(ps) and ps[0] == "cfg"

Try / catch

try:
    obj = MyClass(cfg)
except TypeError as e:
    if "must take 'cfg'" in str(e):
        raise SystemExit("fix from_config signature to from_config(cls, cfg)")
    raise

Prevention

When it happens

Trigger: Writing def from_config(cls, config) or def from_config(cls, model_cfg) — any first arg not literally named 'cfg' — then constructing the class with MyComponent(cfg=cfg) or positional cfg.

Common situations: Renaming the parameter for style reasons; porting code from another framework that uses 'config'; refactor tooling that renames parameters.

Related errors


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