diff --git a/autonerves/setup_colab.py b/autonerves/setup_colab.py index 0914258..33545aa 100644 --- a/autonerves/setup_colab.py +++ b/autonerves/setup_colab.py @@ -16,6 +16,7 @@ """ import logging +import sys from pathlib import Path logger = logging.getLogger(__name__) @@ -137,6 +138,27 @@ def check_jax_using_gpu(log_gpu_warning: bool = True) -> bool: return no_gpu +def _running_in_ipython() -> bool: + """ + True only when an IPython interactive shell (Jupyter notebook, IPython + kernel or terminal) is driving this process. + + A notebook has already imported IPython by the time any cell runs, so a + ``sys.modules`` lookup distinguishes the notebook path from a plain + interpreter without importing IPython (which may be installed but idle) + on every CLI run. + """ + ipython = sys.modules.get("IPython") + + if ipython is None: + return False + + try: + return ipython.get_ipython() is not None + except AttributeError: + return False + + def _installed_version(top_package: str): """The installed version string of ``top_package``, or None if unknown.""" try: @@ -214,8 +236,7 @@ def _colab_setup( try: import google.colab # noqa: F401 except ImportError: - print( - f""" + message = f""" You are not running in a Google Colab environment so cannot use the setup_colab() function. You should therefore have {project_name} installed locally in your environment already (e.g. via pip or @@ -223,7 +244,15 @@ def _colab_setup( You may now continue running your script or Notebook. """ - ) + + # In a notebook the user just ran a setup cell that was a no-op and + # needs telling they can carry on; on a plain CLI run the same text is + # noise on every run, so it drops to DEBUG where it stays retrievable. + if _running_in_ipython(): + print(message) + else: + logger.debug(message) + return # JAX performance tweak, applied only on Colab. diff --git a/test_autonerves/test_setup_colab.py b/test_autonerves/test_setup_colab.py index 36cca7e..db46b80 100644 --- a/test_autonerves/test_setup_colab.py +++ b/test_autonerves/test_setup_colab.py @@ -1,3 +1,4 @@ +import logging import subprocess import sys import types @@ -8,6 +9,24 @@ from autonerves import setup_colab +@pytest.fixture(name="no_ipython") +def make_no_ipython(monkeypatch): + """A plain-interpreter run: no IPython module loaded at all.""" + monkeypatch.delitem(sys.modules, "IPython", raising=False) + + +@pytest.fixture(name="fake_ipython") +def make_fake_ipython(monkeypatch): + """ + Stub the ``IPython`` module a live notebook / kernel would have loaded. + ``get_ipython`` returns a shell object, marking the session interactive. + """ + module = types.ModuleType("IPython") + module.get_ipython = lambda: object() + monkeypatch.setitem(sys.modules, "IPython", module) + return module + + class FakeDevice: def __init__(self, kind): self.kind = kind @@ -94,14 +113,43 @@ def test_import_does_not_set_xla_flags(self): class TestOutsideColab: - def test_setup_is_a_noop(self, capsys): - # google.colab is not importable here, so setup() must return cleanly - # without installing or cloning anything. + def test_cli_run_is_a_silent_noop(self, no_ipython, capsys): + # The Witness: google.colab is not importable and no notebook shell is + # active (a plain CLI run of a workspace start_here.py), so setup() + # must return cleanly without installing or cloning anything AND + # without printing the "not running in a Google Colab" block. + with mock.patch.object(subprocess, "check_call") as check_call: + setup_colab.setup("autolens") + check_call.assert_not_called() + assert capsys.readouterr().out == "" + + def test_notebook_run_still_surfaces_the_message(self, fake_ipython, capsys): + # In a notebook the setup cell was a no-op and the user needs telling + # they can carry on — the message must survive there. with mock.patch.object(subprocess, "check_call") as check_call: setup_colab.setup("autolens") check_call.assert_not_called() assert "not running in a Google Colab" in capsys.readouterr().out + def test_imported_but_inactive_ipython_stays_silent( + self, fake_ipython, capsys + ): + # IPython merely importable (or imported by a library) with no shell + # driving the process is still the CLI path. + fake_ipython.get_ipython = lambda: None + setup_colab.setup("autolens") + assert capsys.readouterr().out == "" + + def test_cli_message_retrievable_at_debug_level(self, no_ipython, caplog): + # The silent path demotes rather than deletes: the text lands on the + # module logger at DEBUG for anyone who asks for it. + with caplog.at_level(logging.DEBUG, logger="autonerves.setup_colab"): + setup_colab.setup("autolens") + assert any( + "not running in a Google Colab" in record.message + for record in caplog.records + ) + def test_wrappers_delegate(self): with mock.patch.object(setup_colab, "setup") as setup_mock: setup_colab.for_autolens(raise_error_if_not_gpu=False)