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
from __future__ import annotations
import asyncio
import copy
import pathlib
import sys
import textwrap
import threading
import types
from queue import Queue
from typing import TYPE_CHECKING, Any
import pytest
from reload_test_utils import random_modname, update_file
import marimo._runtime.reload.module_watcher as mw
from marimo._config.config import DEFAULT_CONFIG
from marimo._dependencies.dependencies import DependencyManager
from marimo._runtime.commands import (
DeleteCellCommand,
UpdateUserConfigCommand,
)
from marimo._runtime.reload.autoreload import ModuleReloader
from marimo._runtime.reload.module_watcher import (
_check_modules,
_depends_on,
_get_excluded_modules,
)
from marimo._runtime.runtime import Kernel
from tests.conftest import ExecReqProvider, MockedKernel
if TYPE_CHECKING:
from collections.abc import Callable
INTERVAL = 0.2
@pytest.fixture(autouse=True)
def _setup_test_sleep():
"""Automatically set up faster sleep interval for all tests in this module"""
old_interval = mw._TEST_SLEEP_INTERVAL
mw._TEST_SLEEP_INTERVAL = INTERVAL
yield
mw._TEST_SLEEP_INTERVAL = old_interval
# these tests use random filenames for modules because they share
# the same sys.modules object, and each test needs fresh modules
@pytest.mark.flaky(reruns=3)
async def test_reload_function(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def foo():
return 1
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(
py_file,
"""
def foo():
return 2
""",
)
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if (
k.graph.cells[er_1.cell_id].stale
and k.graph.cells[er_2.cell_id].stale
):
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
@pytest.mark.flaky(reruns=5)
async def test_disable_and_reenable_reload(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
# tests a bug in which after disabling the reloader, it couldn't be
# re-enabled
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def foo():
return 1
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
# enable reloading ...
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
# disable it ...
config["runtime"]["auto_reload"] = "off"
k.set_user_config(UpdateUserConfigCommand(config=config))
# TODO: Invesitigate flaky on minimal CI
await asyncio.sleep(INTERVAL / 2)
# ... and re-enable it
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(
py_file,
"""
def foo():
return 2
""",
)
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if (
k.graph.cells[er_1.cell_id].stale
and k.graph.cells[er_2.cell_id].stale
):
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
@pytest.mark.flaky(reruns=3)
async def test_reload_nested_module_function(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
b = tmp_path / (b_name := random_modname())
b.mkdir()
(b / "__init__.py").write_text("")
c = b / (c_name := random_modname())
c.mkdir()
(c / "__init__.py").write_text("")
nested_module = c / "mod.py"
nested_module.write_text("func = lambda: 1")
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
f"""
from {b_name}.{c_name}.mod import func
def foo():
return func()
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(nested_module, "func = lambda : 2")
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if (
k.graph.cells[er_1.cell_id].stale
and k.graph.cells[er_2.cell_id].stale
):
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
@pytest.mark.flaky(reruns=3)
async def test_reload_nested_module_import_module(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
b = tmp_path / (b_name := random_modname())
b.mkdir()
(b / "__init__.py").write_text("")
c = b / (c_name := random_modname())
c.mkdir()
(c / "__init__.py").write_text("")
nested_module = c / "mod.py"
nested_module.write_text("func = lambda: 1")
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
f"""
from {b_name}.{c_name} import mod
def foo():
return mod.func()
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(nested_module, "func = lambda : 2")
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if (
k.graph.cells[er_1.cell_id].stale
and k.graph.cells[er_2.cell_id].stale
):
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
async def test_reload_nested_module_import_module_autorun(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
b = tmp_path / (b_name := random_modname())
b.mkdir()
(b / "__init__.py").write_text("")
c = b / (c_name := random_modname())
c.mkdir()
(c / "__init__.py").write_text("")
nested_module = c / "mod.py"
nested_module.write_text("func = lambda: 1")
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
f"""
from {b_name}.{c_name} import mod
def foo():
return mod.func()
"""
)
)
queue = Queue()
k.enqueue_control_request = lambda req: queue.put(req)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "autorun"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(nested_module, "func = lambda: 2")
# wait for the watcher to pick up the change
retries = 0
while retries < 15:
await asyncio.sleep(INTERVAL)
retries += 1
if k.graph.cells[er_1.cell_id].stale:
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
@pytest.mark.flaky(reruns=3)
async def test_reload_package(
tmp_path: pathlib.Path,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
b = tmp_path / (b_name := random_modname())
b.mkdir()
c = b / (c_name := random_modname())
c.mkdir()
(b / "__init__.py").write_text(f"from {b_name}.{c_name}.mod import func")
(c / "__init__.py").write_text("")
nested_module = c / "mod.py"
nested_module.write_text("func = lambda: 1")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"import {b_name}"),
er_2 := exec_req.get(f"x = {b_name}.func()"),
er_3 := exec_req.get("pass"),
]
)
assert not k.errors
assert k.globals[b_name]
assert k.globals["x"] == 1
update_file(nested_module, "func = lambda : 2")
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if (
k.graph.cells[er_1.cell_id].stale
and k.graph.cells[er_2.cell_id].stale
):
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
@pytest.mark.skipif(
not DependencyManager.numpy.has(), reason="NumPy not installed"
)
async def test_reload_third_party(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
# make sure that importing a third-party package like numpy doesn't
# break the module finder
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
import numpy as np
def foo():
return 1
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(
py_file,
"""
def foo():
return 2
""",
)
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if k.graph.cells[er_1.cell_id].stale:
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
@pytest.mark.flaky(reruns=3)
async def test_reload_with_modified_cell(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def foo():
return 1
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 1
update_file(
py_file,
"""
def foo():
return 2
""",
)
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if (
k.graph.cells[er_1.cell_id].stale
and k.graph.cells[er_2.cell_id].stale
):
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
# modify the first cell and make sure it is still marked as stale;
k._maybe_register_cell(
er_1.cell_id, f"from {py_modname} import foo; 1", stale=False
)
assert er_1.cell_id in k.graph.get_stale()
@pytest.mark.xfail(
True,
reason=(
"watcher sometimes takes a long time to pick up file change to "
"an import block on CI"
),
strict=False,
)
async def test_reload_function_in_import_block(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def foo():
return 1
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
# We will modify py_modname but not "random" ...
er_1 := exec_req.get(
f"import random; from {py_modname} import foo"
),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
er_4 := exec_req.get("y = random.randint(0, 10000000)"),
er_5 := exec_req.get("random, foo"),
]
)
assert k.globals["x"] == 1
y = k.globals["y"]
update_file(
py_file,
"""
def foo():
return 2
""",
)
# wait for the watcher to pick up the change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if k.graph.cells[er_1.cell_id].stale:
break
assert k.graph.cells[er_1.cell_id].stale
assert k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
# "random" wasn't modified, so it should be pruned from descendants
assert not k.graph.cells[er_4.cell_id].stale
# er_5 depends on random and foo, and foo is stale
assert k.graph.cells[er_5.cell_id].stale
await k.run_stale_cells()
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
assert not k.graph.cells[er_3.cell_id].stale
assert k.globals["x"] == 2
assert k.globals["y"] == y
@pytest.mark.flaky(reruns=3)
async def test_reload_self_import_cycle(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
k = execution_kernel
sys.path.append(str(tmp_path))
py_function_file = tmp_path / pathlib.Path(py_modname + "_helper" + ".py")
py_function_file.write_text(
textwrap.dedent(
f"""
from {py_modname} import func
def foo():
return func()*2
"""
)
)
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def func():
return 5
"""
)
)
k.app_metadata.filename = str(py_file)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname}_helper import foo"),
er_2 := exec_req.get("x = foo()"),
]
)
assert k.globals["x"] == 10
update_file(py_file, "def func(): return 2")
assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
assert k.graph.cells[er_2.cell_id].stale
await k.run_stale_cells()
assert k.globals["x"] == 4
def _park_watcher(
monkeypatch: pytest.MonkeyPatch, reloader: ModuleReloader, modname: str
) -> tuple[threading.Event, threading.Event]:
"""Pause the watcher before its next scan, once it has an mtime baseline
for `modname`.
`parked` is set while the watcher waits. Set `release` to let it scan.
"""
parked = threading.Event()
release = threading.Event()
real_check_modules = mw._check_modules
def gated(**kwargs: Any) -> dict[str, int]:
if modname in reloader.watcher_modules_mtimes and not release.is_set():
parked.set()
release.wait(timeout=10)
return real_check_modules(**kwargs)
monkeypatch.setattr(mw, "_check_modules", gated)
return parked, release
def _park_watcher_in_crawl(
monkeypatch: pytest.MonkeyPatch,
) -> tuple[threading.Event, threading.Event]:
"""Pause the watcher inside its dependency crawl, between detecting a
change and marking cells stale.
`parked` is set while the watcher waits. Set `release` to let it mark.
"""
parked = threading.Event()
release = threading.Event()
real_depends_on = mw._depends_on
def held(**kwargs: Any) -> bool:
parked.set()
release.wait(timeout=10)
return real_depends_on(**kwargs)
monkeypatch.setattr(mw, "_depends_on", held)
return parked, release
async def _wait_for(predicate: Callable[[], bool], polls: int = 25) -> bool:
for _ in range(polls):
await asyncio.sleep(INTERVAL)
if predicate():
return True
return predicate()
async def test_reload_self_import_cycle_race_with_other_cell(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""A cell run that reloads the edited module must not hide the edit
from the watcher. Cells that import dependents of the module are still
marked stale (#10191)."""
k = execution_kernel
sys.path.append(str(tmp_path))
py_function_file = tmp_path / pathlib.Path(py_modname + "_helper" + ".py")
py_function_file.write_text(
textwrap.dedent(
f"""
from {py_modname} import func
def foo():
return func()*2
"""
)
)
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def func():
return 5
"""
)
)
k.app_metadata.filename = str(py_file)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "autorun"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname}_helper import foo"),
er_2 := exec_req.get("x = foo()"),
er_3 := exec_req.get("pass"),
]
)
assert k.globals["x"] == 10
reloader = k.autoreload_manager._reloader
assert reloader is not None
parked, release = _park_watcher(monkeypatch, reloader, py_modname)
assert await _wait_for(parked.is_set)
# The unrelated cell's run reloads the edited module first and consumes
# the mtime change the kernel tracks.
update_file(py_file, "def func(): return 2")
await k.run([exec_req.get_with_id(er_3.cell_id, "pass")])
assert not k.graph.cells[er_1.cell_id].stale
release.set()
assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
assert k.graph.cells[er_2.cell_id].stale
await k.run_stale_cells()
assert k.globals["x"] == 4
class TestIsSubmodule:
"""Unit tests for the is_submodule utility function"""
def test_parent_child_relationship(self):
from marimo._runtime.reload.module_watcher import is_submodule
# Parent is a submodule of child
assert is_submodule("marimo", "marimo.plugins")
assert is_submodule("marimo.plugins", "marimo.plugins.ui")
assert is_submodule("a.b", "a.b.c.d")
def test_exact_match(self):
from marimo._runtime.reload.module_watcher import is_submodule
# Module is a submodule of itself
assert is_submodule("marimo", "marimo")
assert is_submodule("marimo.plugins", "marimo.plugins")
assert is_submodule("a.b.c", "a.b.c")
def test_not_submodule(self):
from marimo._runtime.reload.module_watcher import is_submodule
# Different modules
assert not is_submodule("marimo", "numpy")
assert not is_submodule("marimo.plugins", "marimo.runtime")
assert not is_submodule("a.b.c", "a.b.d")
def test_longer_src_than_target(self):
from marimo._runtime.reload.module_watcher import is_submodule
# Src is longer than target - can't be parent
assert not is_submodule("marimo.plugins.ui", "marimo.plugins")
assert not is_submodule("marimo.plugins", "marimo")
assert not is_submodule("a.b.c", "a.b")
def test_similar_names_not_submodule(self):
from marimo._runtime.reload.module_watcher import is_submodule
# Make sure "foo" doesn't match "foobar"
assert not is_submodule("foo", "foobar")
assert not is_submodule("marimo", "marimo_extra")
assert not is_submodule("a.b", "a.bc")
def test_empty_strings(self):
from marimo._runtime.reload.module_watcher import is_submodule
# Edge cases with empty strings
assert is_submodule("", "")
# Empty string is not a valid parent module
assert not is_submodule("", "marimo")
assert not is_submodule("marimo", "")
class TestIsThirdPartyModule:
"""Unit tests for the _is_third_party_module utility function"""
def test_module_without_file(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# Module without __file__ attribute
mod = types.ModuleType("test_module")
assert not _is_third_party_module(mod)
def test_module_with_none_file(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# Module with __file__ = None
mod = types.ModuleType("test_module")
mod.__file__ = None
assert not _is_third_party_module(mod)
def test_site_packages_module(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# Module in site-packages
mod = types.ModuleType("test_module")
mod.__file__ = "/usr/lib/python3.10/site-packages/numpy/__init__.py"
assert _is_third_party_module(mod)
def test_local_module(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# Local module not in site-packages
mod = types.ModuleType("test_module")
mod.__file__ = "/home/user/project/mymodule.py"
assert not _is_third_party_module(mod)
def test_site_packages_in_path_but_not_directory(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# Edge case: "site-packages" appears in filename but not as directory
mod = types.ModuleType("test_module")
mod.__file__ = "/home/user/site-packages.py"
assert not _is_third_party_module(mod)
def test_actual_third_party_module(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# pytest should be in site-packages
assert _is_third_party_module(pytest)
def test_builtin_module(self):
from marimo._runtime.reload.module_watcher import (
_is_third_party_module,
)
# Builtin modules typically don't have __file__ or have None
assert not _is_third_party_module(sys)
class TestDependsOn:
"""Unit tests for the _depends_on function"""
def test_depends_on_direct_match(self):
"""Test _depends_on returns True when src is in target_modules"""
src_module = types.ModuleType("src")
target_module = types.ModuleType("target")
target_modules = {src_module, target_module}
target_filenames = set()
reloader = ModuleReloader()
result = _depends_on(
src_module, target_modules, target_filenames, set(), reloader
)
assert result is True
def test_depends_on_by_filename(self, tmp_path: pathlib.Path):
"""Test _depends_on detects dependency by filename"""
# Create a module with a file
py_file = tmp_path / "test_mod.py"
py_file.write_text("x = 1")
src_module = types.ModuleType("src")
src_module.__file__ = str(py_file)
target_modules = set()
target_filenames = {str(py_file)}
reloader = ModuleReloader()
result = _depends_on(
src_module, target_modules, target_filenames, set(), reloader
)
assert result is True
def test_depends_on_package_submodule(self):
"""Test _depends_on detects package dependencies"""
# Create a package module (ends with __init__.py)
src_module = types.ModuleType("mypackage")
src_module.__file__ = "/path/to/mypackage/__init__.py"
src_module.__name__ = "mypackage"
target_module = types.ModuleType("mypackage.submodule")
target_module.__name__ = "mypackage.submodule"
target_module.__file__ = "/path/to/mypackage/submodule.py"
target_modules = {target_module}
target_filenames = {"/path/to/mypackage/submodule.py"}
reloader = ModuleReloader()
result = _depends_on(
src_module, target_modules, target_filenames, set(), reloader
)
assert result is True
def test_depends_on_no_dependency(self):
"""Test _depends_on returns False when no dependency"""
src_module = types.ModuleType("src")
src_module.__file__ = "/path/to/src.py"
target_module = types.ModuleType("target")
target_module.__file__ = "/path/to/target.py"
target_modules = {target_module}
target_filenames = {"/path/to/target.py"}
reloader = ModuleReloader()
result = _depends_on(
src_module, target_modules, target_filenames, set(), reloader
)
assert result is False
def test_depends_on_module_without_file(self):
"""Test _depends_on with module without __file__"""
src_module = types.ModuleType("src")
# No __file__ attribute
target_modules = set()
target_filenames = set()
reloader = ModuleReloader()
result = _depends_on(
src_module, target_modules, target_filenames, set(), reloader
)
assert result is False
class TestGetExcludedModules:
"""Unit tests for the _get_excluded_modules function"""
def test_get_excluded_modules_filters_site_packages(self):
"""Test _get_excluded_modules returns site-packages modules"""
mod1 = types.ModuleType("local_mod")
mod1.__file__ = "/home/user/project/local_mod.py"
mod2 = types.ModuleType("third_party")
mod2.__file__ = "/usr/lib/python3.10/site-packages/third_party.py"
modules = {"local_mod": mod1, "third_party": mod2}
result = _get_excluded_modules(modules)
assert "third_party" in result
assert "local_mod" not in result
def test_get_excluded_modules_always_excludes_marimo_package(self):
"""Editable marimo is not under site-packages; still exclude it (#10225)."""
from marimo._runtime.reload import module_watcher as mw
# Bust cache from previous tests
mw._excluded_modules_cache = None
local = types.ModuleType("local_mod")
local.__file__ = "/home/user/project/local_mod.py"
marimo_mod = types.ModuleType("marimo")
marimo_mod.__file__ = "/home/user/dev/marimo/marimo/__init__.py"
marimo_sub = types.ModuleType("marimo._runtime")
marimo_sub.__file__ = (
"/home/user/dev/marimo/marimo/_runtime/__init__.py"
)
modules = {
"local_mod": local,
"marimo": marimo_mod,
"marimo._runtime": marimo_sub,
}
result = _get_excluded_modules(modules)
assert "marimo" in result
assert "marimo._runtime" in result
assert "local_mod" not in result
def test_is_marimo_internal_module_name(self):
from marimo._runtime.reload.module_watcher import (
_is_marimo_internal_module_name,
)
assert _is_marimo_internal_module_name("marimo")
assert _is_marimo_internal_module_name("marimo._runtime")
assert not _is_marimo_internal_module_name("marimo_tools")
assert not _is_marimo_internal_module_name("local_mod")
def test_get_excluded_modules_caching(self):
"""Test _get_excluded_modules uses cache"""
mod1 = types.ModuleType("mod1")
mod1.__file__ = "/usr/lib/python3.10/site-packages/mod1.py"
modules = {"mod1": mod1}
# First call should compute
result1 = _get_excluded_modules(modules)
assert "mod1" in result1
# Second call with same modules should use cache
result2 = _get_excluded_modules(modules)
assert result2 is result1 # Same object reference
def test_get_excluded_modules_cache_invalidation(self):
"""Test _get_excluded_modules cache updates with different modules"""
mod1 = types.ModuleType("mod1")
mod1.__file__ = "/usr/lib/python3.10/site-packages/mod1.py"
modules1 = {"mod1": mod1}
result1 = _get_excluded_modules(modules1)
# Different set of modules should recompute
mod2 = types.ModuleType("mod2")
mod2.__file__ = "/usr/lib/python3.10/site-packages/mod2.py"
modules2 = {"mod2": mod2}
result2 = _get_excluded_modules(modules2)
assert result1 != result2
assert "mod1" in result1
assert "mod2" in result2
def test_get_excluded_modules_empty(self):
"""Local-only modules still exclude bare marimo for modulefinder."""
from marimo._runtime.reload import module_watcher as mw
mw._excluded_modules_cache = None
mod1 = types.ModuleType("local_mod")
mod1.__file__ = "/home/user/project/local_mod.py"
modules = {"local_mod": mod1}
result = _get_excluded_modules(modules)
# Root package is always excluded even when not present in modules.
assert result == {"marimo"}
class TestCheckModules:
"""Unit tests for the _check_modules function"""
def test_check_modules_detects_stale(self, tmp_path: pathlib.Path):
"""Test _check_modules detects stale modules"""
import importlib
sys.path.append(str(tmp_path))
py_file = tmp_path / "test_check_mod.py"
py_file.write_text("x = 1")
mod = importlib.import_module("test_check_mod")
reloader = ModuleReloader()
reloader.check(sys.modules, reload=False)
# Modify the file
update_file(py_file, "x = 2")
# _check_modules should detect it's stale
modules = {"test_check_mod": mod}
stale = _check_modules(modules, reloader, sys.modules)
assert "test_check_mod" in stale
def test_check_modules_no_stale(self):
"""Test _check_modules with no stale modules"""
reloader = ModuleReloader()
modules = {"os": sys.modules["os"]}
stale = _check_modules(modules, reloader, sys.modules)
assert len(stale) == 0
def test_check_modules_empty_input(self):
"""Test _check_modules with empty modules dict"""
reloader = ModuleReloader()
modules = {}
stale = _check_modules(modules, reloader, sys.modules)
assert len(stale) == 0
class TestModuleWatcherStop:
"""Tests for ModuleWatcher.stop method"""
async def test_module_watcher_stop(
self, execution_kernel: Kernel, exec_req: ExecReqProvider
):
"""Test that ModuleWatcher.stop sets the exit event"""
del exec_req
k = execution_kernel
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
# Give watcher time to start
await asyncio.sleep(INTERVAL)
# Stop the watcher
watcher = k.autoreload_manager.watcher
assert watcher is not None
assert not watcher.should_exit.is_set()
watcher.stop()
# should_exit should be set
assert watcher.should_exit.is_set()
async def test_module_watcher_processes_flag(
self, execution_kernel: Kernel, exec_req: ExecReqProvider
):
"""Test ModuleWatcher run_is_processed flag behavior"""
del exec_req
k = execution_kernel
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
# Give watcher time to start
await asyncio.sleep(INTERVAL)
watcher = k.autoreload_manager.watcher
assert watcher is not None
# Initially should be set (no run in flight)
assert watcher.run_is_processed.is_set()
class TestModuleWatcherEdgeCases:
"""Tests for edge cases in module watching"""
@pytest.mark.flaky(reruns=3)
async def test_module_watcher_handles_deleted_cell(
self,
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
"""Test watcher handles cells being deleted from graph"""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
textwrap.dedent(
"""
def foo():
return 1
"""
)
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
]
)
assert k.globals["x"] == 1
# Delete a cell from the graph
await k.delete_cell(er_2)
# Modify the file
update_file(
py_file,
"""
def foo():
return 2
""",
)
# Wait for watcher to pick up change
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if k.graph.cells[er_1.cell_id].stale:
break
# Only er_1 should be stale (er_2 was deleted)
assert k.graph.cells[er_1.cell_id].stale
assert er_2.cell_id not in k.graph.cells
@pytest.mark.flaky(reruns=3)
async def test_module_watcher_cache_invalidation(
self,
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
"""Test that cell modules cache is properly invalidated"""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("def foo(): return 1")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
# Run a cell with import
await k.run([er_1 := exec_req.get(f"from {py_modname} import foo")])
# Modify the module
update_file(py_file, "def foo(): return 2")
# Wait for watcher
retries = 0
while retries < 10:
await asyncio.sleep(INTERVAL)
retries += 1
if k.graph.cells[er_1.cell_id].stale:
break
assert k.graph.cells[er_1.cell_id].stale
# Run stale cells
await k.run_stale_cells()
# Now modify the cell's imports (simulate changing the import)
# This should invalidate the cache
await k.run(
[exec_req.get(f"from {py_modname} import foo; import sys")]
)
# The cache should handle the modified imports correctly
assert not k.graph.cells[er_1.cell_id].stale
class TestAutoreloadManagerLifecycle:
"""Tests for AutoreloadManager responding to runtime.auto_reload changes."""
async def test_mode_swap_replaces_watcher(
self, execution_kernel: Kernel, exec_req: ExecReqProvider
):
del exec_req
k = execution_kernel
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
lazy_watcher = k.autoreload_manager.watcher
assert lazy_watcher is not None
assert lazy_watcher.mode == "lazy"
config["runtime"]["auto_reload"] = "autorun"
k.set_user_config(UpdateUserConfigCommand(config=config))
autorun_watcher = k.autoreload_manager.watcher
assert autorun_watcher is not None
assert autorun_watcher.mode == "autorun"
assert autorun_watcher is not lazy_watcher
assert lazy_watcher.should_exit.is_set()
async def test_reloader_persists_across_mode_swap(
self, execution_kernel: Kernel, exec_req: ExecReqProvider
):
"""Switching between lazy and autorun must not throw away the
reloader's mtime state — otherwise every swap would force a reload
of all already-tracked modules."""
del exec_req
k = execution_kernel
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
reloader = k.autoreload_manager.reloader
assert reloader is not None
config["runtime"]["auto_reload"] = "autorun"
k.set_user_config(UpdateUserConfigCommand(config=config))
assert k.autoreload_manager.reloader is reloader
async def test_disable_clears_manager_state(
self, execution_kernel: Kernel, exec_req: ExecReqProvider
):
del exec_req
k = execution_kernel
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
assert k.autoreload_manager.watcher is not None
assert k.autoreload_manager.reloader is not None
config["runtime"]["auto_reload"] = "off"
k.set_user_config(UpdateUserConfigCommand(config=config))
assert k.autoreload_manager.watcher is None
assert k.autoreload_manager.reloader is None
async def test_run_rearms_watcher_run_is_processed(
self, execution_kernel: Kernel, exec_req: ExecReqProvider
):
"""The on_finish hook fires for every kernel run, not just
`_run_stale_cells`. This is a deliberate semantic change from the
pre-extraction code, which only set the flag at the end of stale-cell
runs."""
k = execution_kernel
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
watcher = k.autoreload_manager.watcher
assert watcher is not None
watcher.run_is_processed.clear()
await k.run([exec_req.get("x = 1")])
assert watcher.run_is_processed.is_set()
async def test_watcher_does_not_restale_cells_rerun_after_reload(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""Cells that rerun while the watcher crawls dependencies are not marked
stale afterwards.
The gap between detection and marking can hold a second save, such as
format-on-save. The kernel then reloads and reruns the importing cells
before the watcher marks them.
"""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("def foo():\n return 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
]
)
assert k.globals["x"] == 1
update_file(py_file, "def foo():\n return 2\n")
assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
parked, release = _park_watcher_in_crawl(monkeypatch)
update_file(py_file, "def foo():\n return 3\n")
assert await _wait_for(parked.is_set)
await k.run_stale_cells()
assert k.globals["x"] == 3
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
release.set()
await asyncio.sleep(5 * INTERVAL)
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
# The late marking must not prune the import block's metadata either.
# Otherwise its next run reruns every descendant.
assert k.graph.cells[er_1.cell_id].import_workspace.imported_defs == {
"foo"
}
async def test_watcher_marks_cell_whose_context_ran_without_rerun(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""A UI callback enters the cell's execution context without running
its code. No rerun occurs, so the watcher still marks the cell stale."""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("def foo():\n return 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run([er_1 := exec_req.get(f"from {py_modname} import foo")])
parked, release = _park_watcher_in_crawl(monkeypatch)
update_file(py_file, "def foo():\n return 2\n")
assert await _wait_for(parked.is_set)
# Non-cell work in the cell's context reloads modules but does not
# rerun the cell.
with k._install_execution_context(er_1.cell_id):
pass
assert not k.graph.cells[er_1.cell_id].stale
release.set()
assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
async def test_watcher_skips_cell_that_reran_before_its_poll(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""Edit a module, then rerun its importing cell before the watcher
polls.
The rerun reloads the module, so the cell holds the new code. The
watcher sees the edit against its own mtime baseline but leaves that
cell and its imported definitions alone. Its reader still holds the
old value and is marked stale.
"""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("def foo():\n return 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import foo"),
er_2 := exec_req.get("x = foo()"),
]
)
assert k.globals["x"] == 1
reloader = k.autoreload_manager._reloader
assert reloader is not None
parked, release = _park_watcher(monkeypatch, reloader, py_modname)
assert await _wait_for(parked.is_set)
update_file(py_file, "def foo():\n return 2\n")
await k.run(
[exec_req.get_with_id(er_1.cell_id, f"from {py_modname} import foo")]
)
assert k.globals["foo"]() == 2
assert not k.graph.cells[er_1.cell_id].stale
assert not k.graph.cells[er_2.cell_id].stale
release.set()
# Wait until the watcher scans the edit, then give it one poll to
# finish marking.
assert await _wait_for(
lambda: (
reloader.watcher_modules_mtimes.get(py_modname)
== py_file.stat().st_mtime
)
)
await asyncio.sleep(2 * INTERVAL)
assert not k.graph.cells[er_1.cell_id].stale, (
"watcher flagged a cell that already reran with the new code"
)
assert k.graph.cells[er_1.cell_id].import_workspace.imported_defs == {
"foo"
}
assert k.graph.cells[er_2.cell_id].stale, (
"reader computed x from the old foo but is not stale"
)
await k.run_stale_cells()
assert k.globals["x"] == 2
async def test_watcher_marks_descendant_when_import_cell_did_not_rerun(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""A descendant that reruns before the watcher marks still reads the
importing cell's old binding, so it must be marked stale with it."""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("value = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import value"),
er_2 := exec_req.get("y = value"),
]
)
assert k.globals["y"] == 1
parked, release = _park_watcher_in_crawl(monkeypatch)
update_file(py_file, "value = 2\n")
assert await _wait_for(parked.is_set)
# The rerun reloads the module but still reads the import cell's old
# binding.
await k.run([exec_req.get_with_id(er_2.cell_id, "y = value")])
assert k.globals["y"] == 1
release.set()
assert await _wait_for(lambda: k.graph.cells[er_1.cell_id].stale)
assert k.graph.cells[er_2.cell_id].stale
await k.run_stale_cells()
assert k.globals["y"] == 2
async def test_watcher_marks_reader_that_ran_before_its_importer(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""Both cells rerun under the new generation, reader first. The
reader still read the old binding, so only the reader is stale."""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("value = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import value"),
er_2 := exec_req.get("y = value"),
]
)
reloader = k.autoreload_manager._reloader
assert reloader is not None
parked, release = _park_watcher(monkeypatch, reloader, py_modname)
assert await _wait_for(parked.is_set)
update_file(py_file, "value = 2\n")
await k.run([exec_req.get_with_id(er_2.cell_id, "y = value")])
await k.run(
[exec_req.get_with_id(er_1.cell_id, f"from {py_modname} import value")]
)
assert k.globals["value"] == 2
assert k.globals["y"] == 1
release.set()
assert await _wait_for(lambda: k.graph.cells[er_2.cell_id].stale)
assert not k.graph.cells[er_1.cell_id].stale
await k.run_stale_cells()
assert k.globals["y"] == 2
async def test_watcher_marks_every_importer_of_a_module(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
):
"""Two cells import the same module under different aliases and a
third reads both. An edit marks both importers and the reader."""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("value = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_a := exec_req.get(f"from {py_modname} import value as a"),
er_c := exec_req.get(f"from {py_modname} import value as c"),
er_b := exec_req.get("y = (a, c)"),
]
)
assert k.globals["y"] == (1, 1)
update_file(py_file, "value = 2\n")
assert await _wait_for(
lambda: all(
k.graph.cells[er.cell_id].stale for er in (er_a, er_c, er_b)
)
), "watcher marked only one importer of the module"
await k.run_stale_cells()
assert k.globals["y"] == (2, 2)
async def test_watcher_survives_cell_deleted_during_crawl(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("value = 1\n")
other_file = tmp_path / pathlib.Path(py_modname + "_other.py")
other_file.write_text("w = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_1 := exec_req.get(f"from {py_modname} import value"),
er_2 := exec_req.get(f"from {py_modname}_other import w"),
]
)
parked, release = _park_watcher_in_crawl(monkeypatch)
update_file(py_file, "value = 2\n")
assert await _wait_for(parked.is_set)
await k.delete_cell(DeleteCellCommand(cell_id=er_1.cell_id))
release.set()
await asyncio.sleep(4 * INTERVAL)
# The watcher thread is still alive and marks later edits.
update_file(other_file, "w = 2\n")
assert await _wait_for(lambda: k.graph.cells[er_2.cell_id].stale)
async def test_watcher_marks_descendants_of_a_stale_reader(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""Rerunning the importer and a grandchild, but not the child between
them, leaves the grandchild holding the child's old output. Running
after the importer does not make a cell current when its parent is
stale."""
k = execution_kernel
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("value = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run(
[
er_a := exec_req.get(f"from {py_modname} import value"),
er_b := exec_req.get("y = value"),
er_c := exec_req.get("z = y"),
]
)
reloader = k.autoreload_manager.reloader
assert reloader is not None
parked, release = _park_watcher(monkeypatch, reloader, py_modname)
assert await _wait_for(parked.is_set)
update_file(py_file, "value = 2\n")
await k.run([exec_req.get_with_id(er_a.cell_id, er_a.code)])
await k.run([exec_req.get_with_id(er_c.cell_id, er_c.code)])
assert k.globals["value"] == 2
assert k.globals["z"] == 1
release.set()
assert await _wait_for(lambda: k.graph.cells[er_b.cell_id].stale)
assert k.graph.cells[er_c.cell_id].stale, "grandchild holds the old y"
assert not k.graph.cells[er_a.cell_id].stale
await k.run_stale_cells()
assert k.globals["z"] == 2
async def test_watcher_checks_fresh_imports_of_a_stale_importer(
tmp_path: pathlib.Path,
py_modname: str,
execution_kernel: Kernel,
exec_req: ExecReqProvider,
monkeypatch: pytest.MonkeyPatch,
):
"""An import block that is stale for one module is still checked for
the module it already reloaded, so the readers of that module are
marked too."""
k = execution_kernel
sys.path.append(str(tmp_path))
first = tmp_path / pathlib.Path(py_modname + ".py")
first.write_text("value = 1\n")
second = tmp_path / pathlib.Path(py_modname + "_second.py")
second.write_text("value = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
code = (
f"from {py_modname} import value as a\n"
f"from {py_modname}_second import value as b"
)
await k.run(
[
er_a := exec_req.get(code),
er_x := exec_req.get("x = a"),
er_y := exec_req.get("y = b"),
]
)
reloader = k.autoreload_manager.reloader
assert reloader is not None
parked, release = _park_watcher(monkeypatch, reloader, py_modname)
assert await _wait_for(parked.is_set)
update_file(first, "value = 2\n")
await k.run([exec_req.get_with_id(er_a.cell_id, code)])
update_file(second, "value = 2\n")
assert (k.globals["a"], k.globals["x"]) == (2, 1)
release.set()
assert await _wait_for(lambda: k.graph.cells[er_a.cell_id].stale)
assert k.graph.cells[er_x.cell_id].stale, "reader of the reloaded module"
assert k.graph.cells[er_y.cell_id].stale
await k.run_stale_cells()
assert (k.globals["x"], k.globals["y"]) == (2, 2)
async def test_reload_in_cell_run_can_create_ui_elements(
tmp_path: pathlib.Path,
py_modname: str,
mocked_kernel: MockedKernel,
exec_req: ExecReqProvider,
):
"""A module that builds a UI element at import reloads inside the
cell's execution context."""
k = mocked_kernel.k
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text(
"import marimo as mo\nslider = mo.ui.slider(0, 10, value=1)\n"
)
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run([er := exec_req.get(f"from {py_modname} import slider")])
assert k.globals["slider"].value == 1
update_file(
py_file,
"import marimo as mo\nslider = mo.ui.slider(0, 10, value=2)\n",
)
await k.run([exec_req.get_with_id(er.cell_id, er.code)])
assert k.graph.cells[er.cell_id].run_result_status == "success"
assert k.globals["slider"].value == 2
async def test_reload_output_goes_to_the_cell(
tmp_path: pathlib.Path,
py_modname: str,
mocked_kernel: MockedKernel,
exec_req: ExecReqProvider,
):
"""Prints and errors raised while a module reloads belong to the cell
whose run triggered the reload."""
k = mocked_kernel.k
sys.path.append(str(tmp_path))
py_file = tmp_path / pathlib.Path(py_modname + ".py")
py_file.write_text("value = 1\n")
config = copy.deepcopy(DEFAULT_CONFIG)
config["runtime"]["auto_reload"] = "lazy"
k.set_user_config(UpdateUserConfigCommand(config=config))
await k.run([er := exec_req.get(f"import {py_modname}")])
mocked_kernel.stdout.messages.clear()
update_file(py_file, "print('reload output')\nvalue = 2\n")
await k.run([exec_req.get_with_id(er.cell_id, er.code)])
assert "reload output" in "".join(mocked_kernel.stdout.messages)
mocked_kernel.stderr.messages.clear()
update_file(py_file, "value = !\n")
await k.run([exec_req.get_with_id(er.cell_id, er.code)])
assert "SyntaxError" in "".join(mocked_kernel.stderr.messages)