A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
tests: cleanup sys.modules patches (#4897)
Myles Scolnick6917fbd01aff
diff --git a/tests/_runtime/test_manage_script_metadata.py b/tests/_runtime/test_manage_script_metadata.pyindex d666825379..4ac013173d 100644--- a/tests/_runtime/test_manage_script_metadata.py+++ b/tests/_runtime/test_manage_script_metadata.py@@ -3,9 +3,8 @@ from __future__ import annotationsimport pathlibimport sys-from types import ModuleTypefrom typing import TYPE_CHECKING, cast-from unittest.mock import AsyncMock, call, patch+from unittest.mock import AsyncMock, Mock, call, patchimport pytest@@ -225,12 +224,11 @@ async def test_manage_script_metadata_pip_noop(assert "" == f.read()[email protected](sys.modules, {"pyodide": Mock()})async def test_install_missing_packages_micropip(mocked_kernel: MockedKernel,) -> None:k = mocked_kernel.k- # Fake put pyodide in sys.modules- sys.modules["pyodide"] = ModuleType("pyodide")with patch("micropip.install", new_callable=AsyncMock) as mock_install:await k.packages_callbacks.install_missing_packages(@@ -245,16 +243,12 @@ async def test_install_missing_packages_micropip(call(["foobar"]),]- # Remove pyodide from sys.modules- del sys.modules["pyodide"]-[email protected](sys.modules, {"pyodide": Mock()})async def test_install_missing_packages_micropip_with_versions(mocked_kernel: MockedKernel,) -> None:k = mocked_kernel.k- # Fake put pyodide in sys.modules- sys.modules["pyodide"] = ModuleType("pyodide")with patch("micropip.install", new_callable=AsyncMock) as mock_install:await k.packages_callbacks.install_missing_packages(@@ -269,10 +263,8 @@ async def test_install_missing_packages_micropip_with_versions(call(["pandas==1.5.0"]),]- # Remove pyodide from sys.modules- del sys.modules["pyodide"]-[email protected](sys.modules, {"pyodide": Mock(), "already_installed": Mock()})async def test_install_missing_packages_micropip_other_modules(mocked_kernel: MockedKernel,) -> None:@@ -281,8 +273,6 @@ async def test_install_missing_packages_micropip_other_modules(k.module_registry.modules = lambda: set({"idk", "done", "already_installed"})- sys.modules["pyodide"] = ModuleType("pyodide")- sys.modules["already_installed"] = ModuleType("already_installed")with patch("micropip.install", new_callable=AsyncMock) as mock_install:await k.packages_callbacks.install_missing_packages(@@ -297,11 +287,8 @@ async def test_install_missing_packages_micropip_other_modules(call(["idk"]),]- # Remove pyodide from sys.modules- del sys.modules["pyodide"]- del sys.modules["already_installed"]-[email protected](sys.modules, {"pyodide": Mock()})async def test_missing_packages_hook(mocked_kernel: MockedKernel,) -> None:@@ -322,8 +309,6 @@ async def test_missing_packages_hook(InstallingPackageAlert.broadcast = mock_broadcast # type: ignoreMissingPackageAlert.broadcast = mock_broadcast # type: ignore- sys.modules["pyodide"] = ModuleType("pyodide")-# Create a mock runner with ModuleNotFoundErrorclass MockRunner:def __init__(self) -> None:@@ -433,8 +418,6 @@ async def test_missing_packages_hook("scipy": "",}- del sys.modules["pyodide"]-def test_missing_packages_hook_pip(mocked_kernel: MockedKernel,diff --git a/tests/_runtime/test_patches.py b/tests/_runtime/test_patches.pyindex 0d0200f9e5..ff41b4d42b 100644--- a/tests/_runtime/test_patches.py+++ b/tests/_runtime/test_patches.py@@ -2,9 +2,9 @@from __future__ import annotationsimport io-from types import ModuleType+import sysfrom typing import TYPE_CHECKING-from unittest.mock import patch+from unittest.mock import Mock, patchimport pytest@@ -193,16 +193,12 @@ async def test_webbrowser_easter_egg(not DependencyManager.polars.has(),reason="Polars is not installed",)[email protected](sys.modules, {"pyodide": Mock()})def test_polars_write_json_patch(tmp_path: Path):- import sys-import polars as plfile_path = tmp_path / "test.json"- # Fake put pyodide in sys.modules- sys.modules["pyodide"] = ModuleType("pyodide")-# Make write_json throw an errorwith patch("polars.DataFrame.write_json",@@ -240,6 +236,3 @@ def test_polars_write_json_patch(tmp_path: Path):# Test it fails againwith pytest.raises(ValueError, match="Test error"):df.write_json(file_path)-- # Remove pyodide from sys.modules- del sys.modules["pyodide"]diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 22c8cd8a68..0a2f1da5e6 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -6,6 +6,7 @@ import sysimport textwrapfrom dataclasses import dataclassfrom typing import TYPE_CHECKING+from unittest.mock import Mock, patchimport pytest@@ -1157,38 +1158,31 @@ class TestExecution:@pytest.mark.skipif(sys.platform == "win32", reason="Windows paths behave differently")+ @patch.dict(+ sys.modules,+ {+ "pyodide": Mock(),+ "js": Mock(+ location="https://marimo-team.github.io/marimo-gh-pages-template/notebooks/assets/worker-BxJ8HeOy.js"+ ),+ },+ )async def test_notebook_location_for_pyodide(self, any_kernel: Kernel, exec_req: ExecReqProvider) -> None:k = any_kernel- import sys- from types import ModuleType-- # Mock pyodide and js modules- sys.modules["pyodide"] = ModuleType("pyodide")- js = ModuleType("js")- js.location = "https://marimo-team.github.io/marimo-gh-pages-template/notebooks/assets/worker-BxJ8HeOy.js"- sys.modules["js"] = js- try:- await k.run(- [- exec_req.get(- "import marimo as mo; loc = mo.notebook_location()"- )- ]- )- assert (- str(k.globals["loc"])- == "https://marimo-team.github.io/marimo-gh-pages-template/notebooks"- )- assert (- str(k.globals["loc"] / "public" / "data.csv")- == "https://marimo-team.github.io/marimo-gh-pages-template/notebooks/public/data.csv"- )- finally:- del sys.modules["pyodide"]- del sys.modules["js"]+ await k.run(+ [exec_req.get("import marimo as mo; loc = mo.notebook_location()")]+ )+ assert (+ str(k.globals["loc"])+ == "https://marimo-team.github.io/marimo-gh-pages-template/notebooks"+ )+ assert (+ str(k.globals["loc"] / "public" / "data.csv")+ == "https://marimo-team.github.io/marimo-gh-pages-template/notebooks/public/data.csv"+ )async def test_notebook_dir_for_unnamed_notebook(self, tmp_path: pathlib.Path, exec_req: ExecReqProvider