{"record":{"id":"abd2deb0c1900828","repo":"Lightning-AI/pytorch-lightning","slug":"pruning-fn-is-expected-to-be-a-str-in-list-pyt","errorCode":null,"errorMessage":"`pruning_fn` is expected to be a str in {list(_PYTORCH_PRUNING_FUNCTIONS.keys())} or a PyTorch `BasePruningMethod`. Found: {pruning_fn}. HINT: if passing a `BasePruningMethod`, pass the class, not an instance","messagePattern":"`pruning_fn` is expected to be a str in (.+?) or a PyTorch `BasePruningMethod`\\. Found: (.+?)\\. HINT: if passing a `BasePruningMethod`, pass the class, not an instance","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/pruning.py","lineNumber":206,"sourceCode":"                if pruning_dim is None:\n                    raise MisconfigurationException(\n                        \"When requesting `structured` pruning, the `pruning_dim` should be provided.\"\n                    )\n                if pruning_fn == \"ln_structured\":\n                    if pruning_norm is None:\n                        raise MisconfigurationException(\n                            \"When requesting `ln_structured` pruning, the `pruning_norm` should be provided.\"\n                        )\n                    pruning_kwargs[\"n\"] = pruning_norm\n                pruning_kwargs[\"dim\"] = pruning_dim\n            pruning_fn = self._create_pruning_fn(pruning_fn, **pruning_kwargs)\n        elif self._is_pruning_method(pruning_fn):\n            if not use_global_unstructured:\n                raise MisconfigurationException(\n                    \"PyTorch `BasePruningMethod` is currently only supported with `use_global_unstructured=True`.\"\n                )\n        else:\n            raise MisconfigurationException(\n                f\"`pruning_fn` is expected to be a str in {list(_PYTORCH_PRUNING_FUNCTIONS.keys())}\"\n                f\" or a PyTorch `BasePruningMethod`. Found: {pruning_fn}.\"\n                \" HINT: if passing a `BasePruningMethod`, pass the class, not an instance\"\n            )\n\n        # need to ignore typing here since pytorch base class does not define the PRUNING_TYPE attribute\n        if use_global_unstructured and pruning_fn.PRUNING_TYPE != \"unstructured\":  # type: ignore\n            raise MisconfigurationException(\n                'Only the \"unstructured\" PRUNING_TYPE is supported with `use_global_unstructured=True`.'\n                f\" Found method {pruning_fn} of type {pruning_fn.PRUNING_TYPE}. \"  # type: ignore[union-attr]\n            )\n\n        self.pruning_fn = pruning_fn\n        self._apply_pruning = apply_pruning\n        self._make_pruning_permanent = make_pruning_permanent\n\n        if not (isinstance(amount, (int, float)) or callable(amount)):\n            raise MisconfigurationException(","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/pruning.py#L188-L224","documentation":"pruning_fn must be either a string naming a PyTorch built-in pruning function or a torch BasePruningMethod subclass. Anything else (an instance of a method, an int, None, an arbitrary callable) hits the final else branch in __init__ and raises MisconfigurationException, with a hint that the class (not an instance) must be passed.","triggerScenarios":"ModelPruning(pruning_fn=prune.l1_unstructured(model...)) — passing the result (tensor/mask) instead of the function; passing prune.L1Unstructured() (an instance) instead of prune.L1Unstructured (the class); passing a plain lambda.","commonSituations":"Confusing the callback API with torch.nn.utils.prune's functional API; instantiating method classes out of habit; passing functools.partial objects.","solutions":["Pass the class, not an instance: pruning_fn=torch.nn.utils.prune.L1Unstructured","Or pass a valid string like 'l1_unstructured'","Wrap genuinely custom logic in a BasePruningMethod subclass (with PRUNING_TYPE set) and pass that class"],"exampleFix":"# before\nimport torch.nn.utils.prune as prune\nModelPruning(pruning_fn=prune.L1Unstructured())\n# after\nModelPruning(pruning_fn=prune.L1Unstructured)  # or pruning_fn='l1_unstructured'","handlingStrategy":"type-guard","validationCode":"import torch.nn.utils.prune as prune\nassert isinstance(pruning_fn, str) or (isinstance(pruning_fn, type) and issubclass(pruning_fn, prune.BasePruningMethod)), 'pass a string name or the method CLASS'","typeGuard":"def is_valid_pruning_fn(fn) -> bool:\n    import torch.nn.utils.prune as prune\n    return isinstance(fn, str) or (isinstance(fn, type) and issubclass(fn, prune.BasePruningMethod))","tryCatchPattern":null,"preventionTips":["Never pass an instance of a pruning method; pass the class or a string name","Subclass BasePruningMethod for custom logic"],"tags":["pytorch-lightning","pruning","pruning-fn","type-error"],"backgroundTag":"wrong-argument-type","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}