Skip to content

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.

fix: syntax error handling in module reloading (#1222)

Akshay Agrawaldf208f35a1a7
diff --git a/marimo/_runtime/reload/autoreload.py b/marimo/_runtime/reload/autoreload.py
index 82930253a1..d0d38435cb 100644
--- a/marimo/_runtime/reload/autoreload.py
+++ b/marimo/_runtime/reload/autoreload.py
@@ -10,6 +10,7 @@ Based on the autoreload extension from the IPython project (BSD-3 Clause).
from __future__ import annotations
import gc
+import io
import os
import sys
import traceback
@@ -21,6 +22,7 @@ from importlib.util import source_from_cache
from typing import Any, Callable, Dict, Generic, List, Tuple, Type, TypeVar
from marimo import _loggers
+from marimo._messaging.tracebacks import write_traceback
LOGGER = _loggers.marimo_logger()
@@ -312,10 +314,19 @@ def superreload(
try:
module = reload(module)
- except:
- # restore module dictionary on failed reload
- if old_dict is not None:
- module.__dict__.update(old_dict)
+ except Exception as e:
+ if isinstance(e, SyntaxError):
+ # User introduced a SyntaxError -- they should be told,
+ # and module dict should not be restored, ie don't fail
+ # silently.
+ tmpio = io.StringIO()
+ traceback.print_exc(file=tmpio)
+ tmpio.seek(0)
+ write_traceback(tmpio.read())
+ else:
+ # restore module dictionary on failed reload
+ if old_dict is not None:
+ module.__dict__.update(old_dict)
raise
# iterate over all objects and update functions & classes
diff --git a/marimo/_runtime/reload/module_watcher.py b/marimo/_runtime/reload/module_watcher.py
index f9891fd12c..043b2e657b 100644
--- a/marimo/_runtime/reload/module_watcher.py
+++ b/marimo/_runtime/reload/module_watcher.py
@@ -65,9 +65,9 @@ def _depends_on(
try:
finder.run_script(src_module.__file__)
except SyntaxError:
- # user introduced a syntax error, maybe; don't
- # exclude this module from future searches
- return True
+ # user introduced a syntax error, maybe; still check if the
+ # module itself has been modified
+ pass
except Exception:
# some modules like numpy fail when called with run_script;
# run_script takes a long time before failing on them, so