{"record":{"id":"61a0f948bfb9c6c2","repo":"facebookresearch/detectron2","slug":"class-with-configurable-must-have-a-from-config","errorCode":null,"errorMessage":"Class with @configurable must have a 'from_config' classmethod.","messagePattern":"Class with @configurable must have a 'from_config' classmethod\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"detectron2/config/config.py","lineNumber":182,"sourceCode":"            class must have a ``from_config`` classmethod which takes `cfg` as\n            the first argument.\n        from_config (callable): the from_config function in usage 2. It must take `cfg`\n            as its first argument.\n    \"\"\"\n\n    if init_func is not None:\n        assert (\n            inspect.isfunction(init_func)\n            and from_config is None\n            and init_func.__name__ == \"__init__\"\n        ), \"Incorrect use of @configurable. Check API documentation for examples.\"\n\n        @functools.wraps(init_func)\n        def wrapped(self, *args, **kwargs):\n            try:\n                from_config_func = type(self).from_config\n            except AttributeError as e:\n                raise AttributeError(\n                    \"Class with @configurable must have a 'from_config' classmethod.\"\n                ) from e\n            if not inspect.ismethod(from_config_func):\n                raise TypeError(\"Class with @configurable must have a 'from_config' classmethod.\")\n\n            if _called_with_cfg(*args, **kwargs):\n                explicit_args = _get_args_from_config(from_config_func, *args, **kwargs)\n                init_func(self, **explicit_args)\n            else:\n                init_func(self, *args, **kwargs)\n\n        return wrapped\n\n    else:\n        if from_config is None:\n            return configurable  # @configurable() is made equivalent to @configurable\n        assert inspect.isfunction(\n            from_config","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/config/config.py#L164-L200","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a from_config classmethod: @classmethod def from_config(cls, cfg): return {'arg': cfg.MODEL.MY_ARG, ...}","Ensure from_config takes 'cfg' as its first parameter (after cls)","Check that no intermediate class in the MRO deletes or misnames from_config"],"exampleFix":"# before\n@configurable\ndef __init__(self, in_ch, out_ch): ...\n# after\n@classmethod\ndef from_config(cls, cfg):\n    return {\"in_ch\": cfg.MODEL.IN_CH, \"out_ch\": cfg.MODEL.OUT_CH}\n@configurable\ndef __init__(self, in_ch, out_ch): ...","handlingStrategy":"type-guard","validationCode":"assert hasattr(MyClass, \"from_config\") and inspect.ismethod(MyClass.from_config), \"class must define from_config classmethod\"","typeGuard":"def is_configurable_ok(cls) -> bool:\n    return callable(getattr(cls, \"from_config\", None)) and inspect.ismethod(cls.from_config)","tryCatchPattern":"try:\n    obj = MyClass(cfg)\nexcept (AttributeError, TypeError) as e:\n    if \"from_config\" in str(e):\n        obj = MyClass(**manual_kwargs)  # bypass with explicit kwargs\n    else:\n        raise","preventionTips":["Use the documented @configurable boilerplate template whenever decorating __init__","Add a unit test that constructs each configurable class from cfg","Static-check: CI test that inspects all @configurable classes for from_config"],"tags":["config","configurable","detectron2","init"],"backgroundTag":"missing-from-config-method","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}