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: put Inspector in typecheck (#4915)

Myles Scolnick272cce3c026a
diff --git a/marimo/_sql/engines/sqlalchemy.py b/marimo/_sql/engines/sqlalchemy.py
index 864764311b..80b7f5206d 100644
--- a/marimo/_sql/engines/sqlalchemy.py
+++ b/marimo/_sql/engines/sqlalchemy.py
@@ -24,7 +24,7 @@ from marimo._types.ids import VariableName
LOGGER = _loggers.marimo_logger()
if TYPE_CHECKING:
- from sqlalchemy import Engine
+ from sqlalchemy import Engine, Inspector
from sqlalchemy.engine.cursor import CursorResult
from sqlalchemy.sql.type_api import TypeEngine
@@ -36,13 +36,14 @@ class SQLAlchemyEngine(SQLEngine):
def __init__(
self, connection: Engine, engine_name: Optional[VariableName] = None
) -> None:
- from sqlalchemy import Inspector, inspect
-
self._engine = connection
self._engine_name = engine_name
self.inspector: Optional[Inspector] = None
try:
+ # May not exist in older versions of SQLAlchemy
+ from sqlalchemy import inspect
+
self.inspector = inspect(self._engine)
except Exception:
LOGGER.warning("Failed to create inspector", exc_info=True)
diff --git a/marimo/_utils/health.py b/marimo/_utils/health.py
index 9b6b3da718..af7cdeb6f0 100644
--- a/marimo/_utils/health.py
+++ b/marimo/_utils/health.py
@@ -10,6 +10,8 @@ from marimo import _loggers
LOGGER = _loggers.marimo_logger()
+TIMEOUT = 10 # seconds
+
def get_node_version() -> Optional[str]:
try:
@@ -19,7 +21,7 @@ def get_node_version() -> Optional[str]:
stderr=subprocess.PIPE,
text=True,
)
- stdout, stderr = process.communicate()
+ stdout, stderr = communicate_with_timeout(process)
if stderr:
return None
if stdout and (stripped := stdout.strip()):
@@ -107,7 +109,7 @@ def get_chrome_version() -> Optional[str]:
stderr=subprocess.PIPE,
text=True,
)
- stdout, stderr = process.communicate()
+ stdout, stderr = communicate_with_timeout(process)
if stderr:
return None
parts = stdout.strip().split()
@@ -125,7 +127,7 @@ def get_chrome_version() -> Optional[str]:
stderr=subprocess.PIPE,
text=True,
)
- stdout, stderr = process.communicate()
+ stdout, stderr = communicate_with_timeout(process)
if stderr:
return None
parts = stdout.strip().split()
@@ -140,7 +142,7 @@ def get_chrome_version() -> Optional[str]:
stderr=subprocess.PIPE,
text=True,
)
- stdout, stderr = process.communicate()
+ stdout, stderr = communicate_with_timeout(process)
if stderr:
return None
parts = stdout.strip().split()
@@ -166,3 +168,13 @@ def get_chrome_version() -> Optional[str]:
def get_python_version() -> str:
return sys.version.split()[0]
+
+
+def communicate_with_timeout(
+ process: subprocess.Popen[str], timeout: float = TIMEOUT
+) -> tuple[str, str]:
+ try:
+ return process.communicate(timeout=timeout)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ return "", "Error: Process timed out"
diff --git a/tests/_utils/test_background_task.py b/tests/_utils/test_background_task.py
index 6b362489ac..42aa0dec55 100644
--- a/tests/_utils/test_background_task.py
+++ b/tests/_utils/test_background_task.py
@@ -83,20 +83,15 @@ async def test_stop_timeout():
assert task.task.done()
- reason="""
- Works locally, but fails in CI.
- RuntimeError: There is no current event loop in thread 'MainThread'
-"""
-)
-def test_stop_sync():
+async def test_stop_sync():
task = SimpleTask()
async def run():
task.start()
- asyncio.run(run())
- task.stop_sync(timeout=0.1)
+ await run()
+ task.stop_sync(timeout=0.5)
assert not task.running
assert task.task is not None
- assert task.task.done()
+ await asyncio.sleep(0) # Wait a tick
+ assert task.task.done(), "Task should be done"