@@ -158,32 +158,17 @@ def test_build_lightning_loggers_unknown_tracker_raises():
158158
159159def test_build_lightning_loggers_mlflow_absent (monkeypatch ):
160160 """ImportError with install hint when mlflow is not installed."""
161- # Simulate mlflow being absent by blocking its import inside Lightning
162- real_import = __builtins__ .__import__ if hasattr (__builtins__ , "__import__" ) else __import__ # type: ignore[attr-defined]
163-
164- def _block_mlflow (name , * args , ** kwargs ):
165- if "MLFlowLogger" in name or (len (args ) >= 3 and "MLFlowLogger" in str (args [2 ])):
166- raise ImportError ("No module named 'mlflow'" )
167- return real_import (name , * args , ** kwargs )
168-
169- # Use monkeypatch on the lightning loggers module directly
170- mock_module = MagicMock ()
171- mock_module .MLFlowLogger .side_effect = ImportError ("No module named 'mlflow'" )
172-
173- import lightning .pytorch .loggers as lpl
174-
175- original_MLFlowLogger = getattr (lpl , "MLFlowLogger" , None )
176-
177- # Patch lightning.pytorch.loggers so that importing MLFlowLogger raises
178- monkeypatch .setitem (sys .modules , "lightning.pytorch.loggers" , None ) # type: ignore[arg-type]
161+ # The guard checks the actual ``mlflow`` package, so simulate its absence.
162+ monkeypatch .setitem (sys .modules , "mlflow" , None ) # type: ignore[arg-type]
179163 cfg = ObservabilityConfig (experiment_trackers = ["mlflow" ])
180164 with pytest .raises (ImportError , match = "pip install 'deeptab\\ [mlflow\\ ]'" ):
181165 build_lightning_loggers (cfg )
182166
183167
184168def test_build_lightning_loggers_tensorboard_absent (monkeypatch ):
185169 """ImportError with install hint when tensorboard is not installed."""
186- monkeypatch .setitem (sys .modules , "lightning.pytorch.loggers" , None ) # type: ignore[arg-type]
170+ # The guard checks ``torch.utils.tensorboard``, so simulate its absence.
171+ monkeypatch .setitem (sys .modules , "torch.utils.tensorboard" , None ) # type: ignore[arg-type]
187172 cfg = ObservabilityConfig (experiment_trackers = ["tensorboard" ])
188173 with pytest .raises (ImportError , match = "pip install 'deeptab\\ [tensorboard\\ ]'" ):
189174 build_lightning_loggers (cfg )
0 commit comments