{"record":{"id":"0a081ddcc4fe071f","repo":"Lightning-AI/pytorch-lightning","slug":"overriding-fabric-run-and-launching-from-the-c","errorCode":null,"errorMessage":"Overriding `Fabric.run()` and launching from the CLI is not allowed. Run the script normally, or change your code to directly call `fabric = Fabric(...); fabric.setup(...)` etc.","messagePattern":"Overriding `Fabric\\.run\\(\\)` and launching from the CLI is not allowed\\. Run the script normally, or change your code to directly call `fabric = Fabric\\(\\.\\.\\.\\); fabric\\.setup\\(\\.\\.\\.\\)` etc\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":1186,"sourceCode":"\n    def _requires_distributed_sampler(self, dataloader: DataLoader) -> bool:\n        return (\n            getattr(self.strategy, \"distributed_sampler_kwargs\", None) is not None\n            and not isinstance(dataloader.sampler, DistributedSampler)\n            and not has_iterable_dataset(dataloader)\n        )\n\n    @staticmethod\n    def _get_distributed_sampler(dataloader: DataLoader, **kwargs: Any) -> DistributedSampler:\n        kwargs.setdefault(\"shuffle\", isinstance(dataloader.sampler, RandomSampler))\n        kwargs.setdefault(\"seed\", int(os.getenv(\"PL_GLOBAL_SEED\", 0)))\n        if isinstance(dataloader.sampler, (RandomSampler, SequentialSampler)):\n            return DistributedSampler(dataloader.dataset, **kwargs)\n        return DistributedSamplerWrapper(dataloader.sampler, **kwargs)\n\n    def _prepare_run_method(self) -> None:\n        if is_overridden(\"run\", self, Fabric) and _is_using_cli():\n            raise TypeError(\n                \"Overriding `Fabric.run()` and launching from the CLI is not allowed. Run the script normally,\"\n                \" or change your code to directly call `fabric = Fabric(...); fabric.setup(...)` etc.\"\n            )\n        # wrap the run method, so we can inject setup logic or spawn processes for the user\n        setattr(self, \"run\", partial(self._wrap_and_launch, self.run))\n\n    def _validate_launched(self) -> None:\n        if not self._launched and not isinstance(self._strategy, (SingleDeviceStrategy, DataParallelStrategy)):\n            raise RuntimeError(\n                \"To use Fabric with more than one device, you must call `.launch()` or use the CLI:\"\n                \" `fabric run --help`.\"\n            )\n\n    def _validate_setup(self, module: nn.Module, optimizers: Sequence[Optimizer]) -> None:\n        self._validate_launched()\n        if isinstance(module, _FabricModule):\n            raise ValueError(\"A model should be passed only once to the `setup` method.\")\n","sourceCodeStart":1168,"sourceCodeEnd":1204,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L1168-L1204","documentation":"In __init__, Fabric wraps self.run to inject setup/spawn logic. If a subclass overrides run() AND the script was started through the CLI, the wrapping would silently bypass the override, so Fabric raises TypeError telling you to either run normally or not rely on overriding run().","triggerScenarios":"Subclassing Fabric and defining a run() method, then executing the script with `fabric run ...`. is_overridden('run', self, Fabric) combined with _is_using_cli() triggers it in __init__.","commonSituations":"Porting a custom Fabric subclass from standalone scripts to the CLI workflow; overriding run() to add hooks.","solutions":["Run the script with plain `python script.py` instead of the CLI","Or remove the run() override and put logic in your launched function or the CLI-invoked flow"],"exampleFix":"# before\nclass MyFabric(Fabric):\n    def run(self): ...  # + launched via `fabric run`\n# after\npython script.py  # run normally, override allowed","handlingStrategy":"validation","validationCode":"# If you override run(), launch with plain python, not the CLI\n# python script.py  (not: fabric run script.py)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't subclass Fabric to add behavior; use composition or the launched function"],"tags":["lightning","fabric","cli","subclass","method-override"],"backgroundTag":"unsupported-override","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}