{"record":{"id":"52d45babb18bef64","repo":"facebookresearch/detectron2","slug":"name-must-take-cfg-as-the-first-argument","errorCode":null,"errorMessage":"{name} must take 'cfg' as the first argument!","messagePattern":"(.+?) must take 'cfg' as the first argument!","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"detectron2/config/config.py","lineNumber":231,"sourceCode":"            return wrapped\n\n        return wrapper\n\n\ndef _get_args_from_config(from_config_func, *args, **kwargs):\n    \"\"\"\n    Use `from_config` to obtain explicit arguments.\n\n    Returns:\n        dict: arguments to be used for cls.__init__\n    \"\"\"\n    signature = inspect.signature(from_config_func)\n    if list(signature.parameters.keys())[0] != \"cfg\":\n        if inspect.isfunction(from_config_func):\n            name = from_config_func.__name__\n        else:\n            name = f\"{from_config_func.__self__}.from_config\"\n        raise TypeError(f\"{name} must take 'cfg' as the first argument!\")\n    support_var_arg = any(\n        param.kind in [param.VAR_POSITIONAL, param.VAR_KEYWORD]\n        for param in signature.parameters.values()\n    )\n    if support_var_arg:  # forward all arguments to from_config, if from_config accepts them\n        ret = from_config_func(*args, **kwargs)\n    else:\n        # forward supported arguments to from_config\n        supported_arg_names = set(signature.parameters.keys())\n        extra_kwargs = {}\n        for name in list(kwargs.keys()):\n            if name not in supported_arg_names:\n                extra_kwargs[name] = kwargs.pop(name)\n        ret = from_config_func(*args, **kwargs)\n        # forward the other arguments to __init__\n        ret.update(extra_kwargs)\n    return ret\n","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/config/config.py#L213-L249","documentation":"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.","triggerScenarios":"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.","commonSituations":"Renaming the parameter for style reasons; porting code from another framework that uses 'config'; refactor tooling that renames parameters.","solutions":["Rename the first parameter (after cls) of from_config to exactly 'cfg'","Keep from_config(cls, cfg, **kwargs) if you need to forward extra overrides","Re-run after fixing; the check is purely on the parameter name at call time"],"exampleFix":"# before\ndef from_config(cls, d2_cfg): return {'x': d2_cfg.MODEL.X}\n# after\ndef from_config(cls, cfg): return {'x': cfg.MODEL.X}","handlingStrategy":"validation","validationCode":"params = list(inspect.signature(MyClass.from_config).parameters)\nassert params[0] == \"cfg\", f\"first param must be 'cfg', got {params[0]}\"","typeGuard":"def from_config_signature_ok(cls) -> bool:\n    ps = list(inspect.signature(cls.from_config).parameters)\n    return bool(ps) and ps[0] == \"cfg\"","tryCatchPattern":"try:\n    obj = MyClass(cfg)\nexcept TypeError as e:\n    if \"must take 'cfg'\" in str(e):\n        raise SystemExit(\"fix from_config signature to from_config(cls, cfg)\")\n    raise","preventionTips":["Keep the literal name 'cfg' as first parameter in every from_config","Never rename the parameter when refactoring","Codify the signature in a lint rule or test"],"tags":["config","configurable","signature","detectron2"],"backgroundTag":"missing-from-config-method","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}