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.

refactor: migrate VariableName to use NewType (#3840)

devin-ai-integration[bot]d31ba1141566
diff --git a/marimo/_data/get_datasets.py b/marimo/_data/get_datasets.py
index 3de318a44d..88eb82b48f 100644
--- a/marimo/_data/get_datasets.py
+++ b/marimo/_data/get_datasets.py
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, List, Optional, cast
from marimo import _loggers
from marimo._data.models import DataTable, DataTableColumn, DataType
from marimo._plugins.ui._impl.tables.utils import get_table_manager_or_none
+from marimo._types.ids import VariableName
LOGGER = _loggers.marimo_logger()
@@ -14,7 +15,7 @@ if TYPE_CHECKING:
def get_datasets_from_variables(
- variables: List[tuple[str, object]],
+ variables: List[tuple[VariableName, object]],
) -> List[DataTable]:
tables: List[DataTable] = []
for variable_name, value in variables:
@@ -25,7 +26,9 @@ def get_datasets_from_variables(
return tables
-def _get_data_table(value: object, variable_name: str) -> Optional[DataTable]:
+def _get_data_table(
+ value: object, variable_name: VariableName
+) -> Optional[DataTable]:
try:
table = get_table_manager_or_none(value)
if table is None:
@@ -79,7 +82,7 @@ def has_updates_to_datasource(query: str) -> bool:
def get_datasets_from_duckdb(
connection: Optional[duckdb.DuckDBPyConnection],
- engine_name: Optional[str] = None,
+ engine_name: Optional[VariableName] = None,
) -> List[DataTable]:
try:
return _get_datasets_from_duckdb_internal(connection, engine_name)
@@ -90,7 +93,7 @@ def get_datasets_from_duckdb(
def _get_datasets_from_duckdb_internal(
connection: Optional[duckdb.DuckDBPyConnection],
- engine_name: Optional[str] = None,
+ engine_name: Optional[VariableName] = None,
) -> List[DataTable]:
# Columns
# 0:"database"
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index 8e6ebcf6a8..97e6eb4faf 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -37,10 +37,12 @@ from marimo._sql.get_engines import (
get_engines_from_variables,
)
from marimo._tracer import kernel_tracer
+from marimo._types.ids import VariableName
from marimo._utils.flatten import contains_instance
LOGGER = _loggers.marimo_logger()
+
PostExecutionHookType = Callable[
[CellImpl, cell_runner.Runner, cell_runner.RunResult], None
]
@@ -126,7 +128,7 @@ def _broadcast_datasets(
del run_result
tables = get_datasets_from_variables(
[
- (variable, runner.glbls[variable])
+ (VariableName(variable), runner.glbls[variable])
for variable in cell.defs
if variable in runner.glbls
]
@@ -149,7 +151,7 @@ def _broadcast_data_source_connection(
del run_result
engines = get_engines_from_variables(
[
- (variable, runner.glbls[variable])
+ (VariableName(variable), runner.glbls[variable])
for variable in cell.defs
if variable in runner.glbls
]
diff --git a/marimo/_sql/engines.py b/marimo/_sql/engines.py
index 7f009ff37e..a9e5185a8f 100644
--- a/marimo/_sql/engines.py
+++ b/marimo/_sql/engines.py
@@ -9,6 +9,7 @@ from marimo._data.models import DataTable, DataTableColumn, DataType
from marimo._dependencies.dependencies import DependencyManager
from marimo._sql.types import SQLEngine
from marimo._sql.utils import wrapped_sql
+from marimo._types.ids import VariableName
LOGGER = _loggers.marimo_logger()
@@ -31,7 +32,7 @@ class DuckDBEngine(SQLEngine):
def __init__(
self,
connection: Optional[duckdb.DuckDBPyConnection],
- engine_name: Optional[str] = None,
+ engine_name: Optional[VariableName] = None,
) -> None:
self._connection = connection
self._engine_name = engine_name
@@ -75,7 +76,7 @@ class SQLAlchemyEngine(SQLEngine):
"""SQLAlchemy engine."""
def __init__(
- self, engine: Engine, engine_name: Optional[str] = None
+ self, engine: Engine, engine_name: Optional[VariableName] = None
) -> None:
self._engine = engine
self._engine_name = engine_name
diff --git a/marimo/_sql/get_engines.py b/marimo/_sql/get_engines.py
index 47c4b882d0..31bfa474df 100644
--- a/marimo/_sql/get_engines.py
+++ b/marimo/_sql/get_engines.py
@@ -6,12 +6,13 @@ from typing import Any, cast
from marimo._data.models import DataSourceConnection
from marimo._sql.engines import DuckDBEngine, SQLAlchemyEngine
from marimo._sql.types import SQLEngine
+from marimo._types.ids import VariableName
def get_engines_from_variables(
- variables: list[tuple[str, object]],
-) -> list[tuple[str, SQLEngine]]:
- engines: list[tuple[str, SQLEngine]] = []
+ variables: list[tuple[VariableName, object]],
+) -> list[tuple[VariableName, SQLEngine]]:
+ engines: list[tuple[VariableName, SQLEngine]] = []
for variable_name, value in variables:
if SQLAlchemyEngine.is_compatible(value):
engines.append(
@@ -34,7 +35,7 @@ def get_engines_from_variables(
def engine_to_data_source_connection(
- variable_name: str,
+ variable_name: VariableName,
engine: SQLEngine,
) -> DataSourceConnection:
return DataSourceConnection(
diff --git a/marimo/_types/ids.py b/marimo/_types/ids.py
index 8083ceb128..dface65fcf 100644
--- a/marimo/_types/ids.py
+++ b/marimo/_types/ids.py
@@ -14,5 +14,4 @@ SessionId = str
ConsumerId = NewType("ConsumerId", str)
-# VariableName = NewType("VariableName", str)
-VariableName = str
+VariableName = NewType("VariableName", str)
diff --git a/marimo/_utils/dataclass_to_openapi.py b/marimo/_utils/dataclass_to_openapi.py
index b02177bab6..d1f8cdf5a3 100644
--- a/marimo/_utils/dataclass_to_openapi.py
+++ b/marimo/_utils/dataclass_to_openapi.py
@@ -52,6 +52,10 @@ class PythonTypeToOpenAPI:
origin = get_origin(py_type)
optional_name_overrides = self.optional_name_overrides
+ # Handle NewType by unwrapping to its base type
+ if hasattr(py_type, "__supertype__"): # NewType check
+ return self.convert(py_type.__supertype__, processed_classes)
+
if origin is Union:
args = get_args(py_type)