{"record":{"id":"98c762c8f19f2c15","repo":"huggingface/transformers","slug":"cls-name-accepts-only-keyword-arguments-but","errorCode":null,"errorMessage":"{cls.__name__} accepts only keyword arguments, but found `{len(args)}` positional args.","messagePattern":"(.+?) accepts only keyword arguments, but found `(.+?)` positional args\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/configuration_utils.py","lineNumber":117,"sourceCode":"    \"\"\"Apply legacy → current layer-type name mapping.\"\"\"\n    return [_LEGACY_LAYER_TYPE_REMAP.get(t, t) for t in layer_types]\n\n\n# copied from huggingface_hub.dataclasses.strict when `accept_kwargs=True`\ndef wrap_init_to_accept_kwargs(cls: dataclass):\n    # Get the original dataclass-generated __init__\n    original_init = cls.__init__\n\n    @wraps(original_init)\n    def __init__(self, *args, **kwargs: Any) -> None:\n        # Extract only the fields that are part of the dataclass\n        dataclass_fields = {f.name for f in fields(cls)}\n        standard_kwargs = {k: v for k, v in kwargs.items() if k in dataclass_fields}\n\n        # We need to call bare `__init__` without `__post_init__` but the `original_init` of\n        # any dataclas contains a call to post-init at the end (without kwargs)\n        if len(args) > 0:\n            raise ValueError(\n                f\"{cls.__name__} accepts only keyword arguments, but found `{len(args)}` positional args.\"\n            )\n\n        for f in fields(cls):  # type: ignore\n            if f.name in standard_kwargs:\n                setattr(self, f.name, standard_kwargs[f.name])\n            elif f.default is not MISSING:\n                setattr(self, f.name, f.default)\n            elif f.default_factory is not MISSING:\n                setattr(self, f.name, f.default_factory())\n            else:\n                raise TypeError(f\"Missing required field - '{f.name}'\")\n\n        # Pass any additional kwargs to `__post_init__` and let the object\n        # decide whether to set the attr or use for different purposes (e.g. BC checks)\n        additional_kwargs = {}\n        for name, value in kwargs.items():\n            if name not in dataclass_fields:","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/configuration_utils.py#L99-L135","documentation":"ValueError raised by the @strict-style dataclass init wrapper used for new-style configs: configuration classes decorated this way accept only keyword arguments. Any positional argument is rejected because field order is not part of the stable API and silent positional binding could construct an invalid config.","triggerScenarios":"Constructing a strict config dataclass positionally, e.g. SomeConfig(768, 12) instead of SomeConfig(hidden_size=768, num_heads=12). Also unpacking a tuple/list into the constructor.","commonSituations":"Porting old code that relied on positional args of a legacy (non-dataclass) config; IDE autocompletion inserting positional placeholders; code generation that emits positional constructors.","solutions":["Pass every argument as a keyword: SomeConfig(hidden_size=768, num_heads=12)","If unpacking a dict, use SomeConfig(**params) rather than SomeConfig(*params)","Check the config class docstring/fields for the exact accepted keyword names"],"exampleFix":"# before\ncfg = MyConfig(768, 12)\n# after\ncfg = MyConfig(hidden_size=768, num_attention_heads=12)","handlingStrategy":"type-guard","validationCode":"import inspect\nsig = inspect.signature(MyConfig)\nassert all(p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY) for p in sig.parameters.values())\ncfg = MyConfig(**params)  # never positional","typeGuard":"def build_strict(cls, params: dict):\n    if not all(isinstance(k, str) for k in params):\n        raise TypeError('strict configs accept keyword arguments only')\n    return cls(**params)","tryCatchPattern":"try:\n    cfg = MyConfig(**params)\nexcept ValueError as e:\n    if 'positional args' in str(e):\n        raise TypeError('rewrite the call with keyword arguments') from e\n    raise","preventionTips":["Always construct configs with keywords; never unpack tuples into config constructors","Lint against ConfigClass(*args) patterns in code review"],"tags":["config","dataclass","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}