{"record":{"id":"8d6fed0a85bce152","repo":"Lightning-AI/pytorch-lightning","slug":"trainer-test-requires-a-lightningmodule-whe","errorCode":null,"errorMessage":"\"`Trainer.test()` requires a `LightningModule` when it hasn't been passed in a previous run\"","messagePattern":"\"`Trainer\\.test\\(\\)` requires a `LightningModule` when it hasn't been passed in a previous run\"","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/trainer.py","lineNumber":817,"sourceCode":"            like :meth:`~lightning.pytorch.LightningModule.test_step` etc.\n            The length of the list corresponds to the number of test dataloaders used.\n\n        Raises:\n            TypeError:\n                If no ``model`` is passed and there was no ``LightningModule`` passed in the previous run.\n                If ``model`` passed is not `LightningModule` or `torch._dynamo.OptimizedModule`.\n\n            MisconfigurationException:\n                If both ``dataloaders`` and ``datamodule`` are passed. Pass only one of these.\n\n            RuntimeError:\n                If a compiled ``model`` is passed and the strategy is not supported.\n\n        \"\"\"\n        if model is None:\n            # do we still have a reference from a previous call?\n            if self.lightning_module is None:\n                raise TypeError(\n                    \"`Trainer.test()` requires a `LightningModule` when it hasn't been passed in a previous run\"\n                )\n        else:\n            model = _maybe_unwrap_optimized(model)\n            self.strategy._lightning_module = model\n        _verify_strategy_supports_compile(self.lightning_module, self.strategy)\n        self.state.fn = TrainerFn.TESTING\n        self.state.status = TrainerStatus.RUNNING\n        self.testing = True\n        return call._call_and_handle_interrupt(\n            self, self._test_impl, model, dataloaders, ckpt_path, verbose, datamodule, weights_only\n        )\n\n    def _test_impl(\n        self,\n        model: Optional[\"pl.LightningModule\"] = None,\n        dataloaders: Optional[Union[EVAL_DATALOADERS, LightningDataModule]] = None,\n        ckpt_path: Optional[_PATH] = None,","sourceCodeStart":799,"sourceCodeEnd":835,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/trainer.py#L799-L835","documentation":"trainer.test() was called with model=None on a Trainer that has no LightningModule reference from an earlier run. test() can only reuse a model if fit/validate/test/predict previously attached one; otherwise the model must be passed explicitly.","triggerScenarios":"Fresh Trainer followed directly by trainer.test(); or constructing a new Trainer for evaluation without loading a model or checkpoint.","commonSituations":"Evaluation-only scripts that assume trainer.test(ckpt_path=...) alone suffices — a model instance is still required; the checkpoint only restores weights.","solutions":["Pass the model: trainer.test(model, ckpt_path=\"best\")","Instantiate from checkpoint: model = LitModel.load_from_checkpoint(...) then trainer.test(model)","Run trainer.fit(model) before trainer.test() on the same Trainer"],"exampleFix":"# before\ntrainer = Trainer()\ntrainer.test(ckpt_path=\"best.ckpt\")\n# after\nmodel = LitModel.load_from_checkpoint(\"best.ckpt\")\ntrainer.test(model)","handlingStrategy":"type-guard","validationCode":"if trainer.lightning_module is None:\n    model = LitModel.load_from_checkpoint(\"best.ckpt\")\nelse:\n    model = trainer.lightning_module\ntrainer.test(model, ckpt_path=\"best\")","typeGuard":"def has_model(t) -> bool:\n    return t.lightning_module is not None","tryCatchPattern":null,"preventionTips":["ckpt_path restores weights but does not create the model — always instantiate it first","Structure eval scripts: build model -> build Trainer -> trainer.test(model)"],"tags":["trainer","test","missing-model","checkpoint","pytorch-lightning"],"backgroundTag":"missing-required-model-argument","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}