{"record":{"id":"41dd10e4782d8979","repo":"Lightning-AI/pytorch-lightning","slug":"the-constructor-name-implementation-has-an-e","errorCode":null,"errorMessage":"The {constructor.__name__} implementation has an error where more than one `__init__` argument can be passed to its parent's `{argument}=...` `__init__` argument. This is likely caused by allowing passing both a custom argument that will map to the `{argument}` argument as well as `**kwargs`. `kwargs` should be filtered to make sure they don't contain the `{argument}` key. This argument was automatically passed to your object by PyTorch Lightning.","messagePattern":"The (.+?) implementation has an error where more than one `__init__` argument can be passed to its parent's `(.+?)=\\.\\.\\.` `__init__` argument\\. This is likely caused by allowing passing both a custom argument that will map to the `(.+?)` argument as well as `\\*\\*kwargs`\\. `kwargs` should be filtered to make sure they don't contain the `(.+?)` key\\. This argument was automatically passed to your object by PyTorch Lightning\\.","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/data.py","lineNumber":275,"sourceCode":"        result = constructor(*args, **kwargs)\n    except TypeError as ex:\n        # improve exception message due to an incorrect implementation of the `DataLoader` where multiple subclass\n        # `__init__` arguments map to one `DataLoader.__init__` argument\n        import re\n\n        match = re.match(r\".*__init__\\(\\) got multiple values .* '(\\w+)'\", str(ex))\n        if not match:\n            # an unexpected `TypeError`, continue failure\n            raise\n        argument = match.groups()[0]\n        message = (\n            f\"The {constructor.__name__} implementation has an error where more than one `__init__` argument\"\n            f\" can be passed to its parent's `{argument}=...` `__init__` argument. This is likely caused by allowing\"\n            f\" passing both a custom argument that will map to the `{argument}` argument as well as `**kwargs`.\"\n            f\" `kwargs` should be filtered to make sure they don't contain the `{argument}` key.\"\n            \" This argument was automatically passed to your object by PyTorch Lightning.\"\n        )\n        raise MisconfigurationException(message) from ex\n\n    attrs_record = getattr(orig_object, \"__pl_attrs_record\", [])\n    for args, fn in attrs_record:\n        fn(result, *args)\n\n    return result\n\n\ndef _wrap_init_method(init: Callable, store_explicit_arg: Optional[str] = None) -> Callable:\n    \"\"\"Wraps the ``__init__`` method of classes (currently :class:`~torch.utils.data.DataLoader` and\n    :class:`~torch.utils.data.BatchSampler`) in order to enable re-instantiation of custom subclasses.\"\"\"\n\n    @functools.wraps(init)\n    def wrapper(obj: Any, *args: Any, **kwargs: Any) -> None:\n        # We need to inspect `init`, as inspecting `obj.__init__`\n        # can lead to inspecting the wrong function with multiple inheritance\n        old_inside_init = getattr(obj, \"__pl_inside_init\", False)\n        object.__setattr__(obj, \"__pl_inside_init\", True)","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/data.py#L257-L293","documentation":"While re-instantiating a wrapped object (dataloader or batch sampler), Lightning caught a TypeError from the constructor indicating a duplicate keyword: the user's class both maps its own argument onto a parent argument (e.g. `ds` passed as `dataset`) and forwards unfiltered `**kwargs` that also contain that key. Lightning converts this into a MisconfigurationException pinpointing the duplicated argument.","triggerScenarios":"A custom class whose `__init__` signature has a custom parameter that it forwards to a parent's parameter (like `dataset` or `sampler`) while also passing `**kwargs` through unchanged; Lightning auto-passes that argument (e.g. when injecting a sampler or dataset), producing 'got multiple values for keyword argument'.","commonSituations":"Wrapper DataLoaders/batch samplers that rename arguments (e.g. `def __init__(self, ds, **kwargs): super().__init__(dataset=ds, **kwargs)`) combined with Lightning's automatic injection.","solutions":["Filter the overlapping key out of kwargs before forwarding: `kwargs.pop('dataset', None)` (or whichever argument the error names).","Don't rename the argument — keep the parent's name (`dataset`) in your `__init__` and forward it directly.","Accept the parent's exact signature (or `*args, **kwargs` only) so no duplicate mapping can occur."],"exampleFix":"# before\nclass MyLoader(DataLoader):\n    def __init__(self, ds, **kwargs):\n        super().__init__(dataset=ds, **kwargs)  # kwargs may contain 'dataset'\n\n# after\nclass MyLoader(DataLoader):\n    def __init__(self, ds, **kwargs):\n        kwargs.pop('dataset', None)\n        super().__init__(dataset=ds, **kwargs)","handlingStrategy":"validation","validationCode":"import inspect\n\ndef has_duplicate_kwarg_forwarding(cls, parent_arg=\"dataset\") -> bool:\n    params = inspect.signature(cls.__init__).parameters\n    has_var_kw = any(p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values())\n    renamed = parent_arg not in params and has_var_kw  # custom arg likely maps to parent_arg\n    return renamed\n\n# if has_duplicate_kwarg_forwarding(MyLoader): fix class before use","typeGuard":null,"tryCatchPattern":"try:\n    fabric.setup_dataloaders(loader)\nexcept MisconfigurationException as e:\n    if \"more than one `__init__` argument\" in str(e):\n        raise  # fix the wrapper class as message instructs\n    raise","preventionTips":["Never rename parent arguments while also forwarding **kwargs; pop overlapping keys.","Keep parent argument names (dataset, sampler, batch_sampler) in wrapper __init__ signatures.","Unit-test wrapper constructors against the exact kwargs Lightning injects."],"tags":["pytorch-lightning","kwargs","duplicate-argument","dataloader"],"backgroundTag":"duplicate-keyword-argument","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}