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.

Upgrade mypy to 1.15.0 and fix type errors (#3966)

devin-ai-integration[bot]c65281007b95
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.py
index b97fc00f44..05bd626b39 100644
--- a/marimo/_ast/compiler.py
+++ b/marimo/_ast/compiler.py
@@ -111,23 +111,23 @@ def fix_source_position(node: Any, source_position: SourcePosition) -> Any:
continue
if "lineno" in child._attributes:
- child.lineno = getattr(child, "lineno", 0) + line_offset
+ child.lineno = getattr(child, "lineno", 0) + line_offset # type: ignore[attr-defined]
if "col_offset" in child._attributes:
- child.col_offset = getattr(child, "col_offset", 0) + col_offset
+ child.col_offset = getattr(child, "col_offset", 0) + col_offset # type: ignore[attr-defined]
if (
"end_lineno" in child._attributes
and (end_lineno := getattr(child, "end_lineno", 0)) is not None
):
- child.end_lineno = end_lineno + line_offset
+ child.end_lineno = end_lineno + line_offset # type: ignore[attr-defined]
if (
"end_col_offset" in child._attributes
and (end_col_offset := getattr(child, "end_col_offset", 0))
is not None
):
- child.end_col_offset = end_col_offset + col_offset
+ child.end_col_offset = end_col_offset + col_offset # type: ignore[attr-defined]
return node
@@ -183,7 +183,7 @@ def compile_cell(
# semicolon. Evaluates expression to "None" otherwise.
if isinstance(final_expr, ast.Expr) and not ends_with_semicolon(code):
expr = ast.Expression(module.body.pop().value)
- expr.lineno = final_expr.lineno
+ expr.lineno = final_expr.lineno # type: ignore[attr-defined]
else:
const = ast.Constant(value=None)
const.col_offset = final_expr.end_col_offset
@@ -191,10 +191,10 @@ def compile_cell(
expr = ast.Expression(const)
# use code over body since lineno corresponds to source
const.lineno = len(code.splitlines()) + 1
- expr.lineno = const.lineno
+ expr.lineno = const.lineno # type: ignore[attr-defined]
# Creating an expression clears source info, so it needs to be set back
- expr.col_offset = final_expr.end_col_offset
- expr.end_col_offset = final_expr.end_col_offset
+ expr.col_offset = final_expr.end_col_offset # type: ignore[attr-defined]
+ expr.end_col_offset = final_expr.end_col_offset # type: ignore[attr-defined]
filename: str
if source_position:
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.py
index e741bb918d..871d7455b7 100644
--- a/marimo/_plugins/stateless/status/_progress.py
+++ b/marimo/_plugins/stateless/status/_progress.py
@@ -358,7 +358,7 @@ class progress_bar:
def __iter__(self) -> Iterable[S | int]:
for item in self.collection:
- yield item
+ yield item # type: ignore[misc]
if not self.disabled:
self.progress.update(increment=self.step)
self._finish()
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index dae0ab0075..9479cbc9e4 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -1197,7 +1197,7 @@ class button(UIElement[Any, Any]):
# frontend will send is 1
return self._initial_value
try:
- return self._on_click(self._value)
+ return self._on_click(self._value) # type: ignore[no-untyped-call]
except Exception:
sys.stderr.write(
f"on_click handler for button ({str(self)}) raised an Exception:\n {traceback.format_exc()}\n"
diff --git a/marimo/_runtime/input_override.py b/marimo/_runtime/input_override.py
index 58c850c649..0362ae72e7 100644
--- a/marimo/_runtime/input_override.py
+++ b/marimo/_runtime/input_override.py
@@ -6,4 +6,4 @@ import sys
@functools.wraps(input)
def input_override(prompt: str = "") -> str:
# sys.stdin is overridden
- return sys.stdin._readline_with_prompt(prompt) # type: ignore[attr-defined, no-any-return] # noqa: E501
+ return sys.stdin._readline_with_prompt(prompt) # type: ignore[attr-defined, no-any-return, union-attr] # noqa: E501
diff --git a/marimo/_runtime/state.py b/marimo/_runtime/state.py
index 31674737f0..45e80124a1 100644
--- a/marimo/_runtime/state.py
+++ b/marimo/_runtime/state.py
@@ -173,9 +173,9 @@ class SetFunctor(Generic[T]):
def __call__(self, update: T | Callable[[T], T]) -> None:
self._state._value = (
- update(self._state._value)
+ update(self._state._value) # type: ignore[assignment]
if isinstance(update, (types.MethodType, types.FunctionType))
- else update
+ else update # type: ignore[assignment]
)
try:
ctx = get_context()
diff --git a/marimo/_save/ast.py b/marimo/_save/ast.py
index e36da9b46e..3b008aa8ed 100644
--- a/marimo/_save/ast.py
+++ b/marimo/_save/ast.py
@@ -22,7 +22,7 @@ def compiled_ast(block: Sequence[ast.AST | ast.stmt]) -> ast.Module:
return cast(
ast.Module,
compile(
- ast.Module(block, type_ignores=[]),
+ ast.Module(cast(list[ast.stmt], block), type_ignores=[]),
# <ast> is non-standard as a filename, but easier to debug than
# <module> everywhere.
"<ast>",
@@ -67,11 +67,11 @@ def clean_to_modules(
if with_block.optional_vars:
initializer = ast.Assign(
targets=[with_block.optional_vars],
- value=initializer,
+ value=cast(ast.expr, initializer),
)
else:
# Edgecase with no "as" clause.
- initializer = ast.Expr(value=initializer)
+ initializer = ast.Expr(value=cast(ast.expr, initializer))
initializer.lineno = len(pre_block) + 1
initializer.col_offset = 0
pre_block.append(initializer)
@@ -214,7 +214,7 @@ class RemoveReturns(ast.NodeTransformer):
# Note that functools caches the generator, which is then dequeue'd,
# so in that sense, it doesn't work either.
def visit_Return(self, node: ast.Return) -> ast.Expr:
- expr = ast.Expr(value=node.value)
+ expr = ast.Expr(value=cast(ast.expr, node.value))
expr.lineno = node.lineno
expr.col_offset = node.col_offset
return expr
diff --git a/marimo/_server/router.py b/marimo/_server/router.py
index 4c2e2aa546..ae5848bdc4 100644
--- a/marimo/_server/router.py
+++ b/marimo/_server/router.py
@@ -66,11 +66,17 @@ class APIRouter(Router):
return response
if dataclasses.is_dataclass(response):
- return JSONResponse(
- content=deep_to_camel_case(
- dataclasses.asdict(response)
+ # Handle both dataclass instance and dataclass type
+ if isinstance(response, type):
+ # If it's a type, we can't call asdict on it
+ return JSONResponse(content=deep_to_camel_case({}))
+ else:
+ # If it's an instance, we can call asdict
+ return JSONResponse(
+ content=deep_to_camel_case(
+ dataclasses.asdict(response)
+ )
)
- )
return JSONResponse(content=json.dumps(response))
@@ -112,11 +118,17 @@ class APIRouter(Router):
return response
if dataclasses.is_dataclass(response):
- return JSONResponse(
- content=deep_to_camel_case(
- dataclasses.asdict(response)
+ # Handle both dataclass instance and dataclass type
+ if isinstance(response, type):
+ # If it's a type, we can't call asdict on it
+ return JSONResponse(content=deep_to_camel_case({}))
+ else:
+ # If it's an instance, we can call asdict
+ return JSONResponse(
+ content=deep_to_camel_case(
+ dataclasses.asdict(response)
+ )
)
- )
return response # type: ignore[no-any-return]
diff --git a/marimo/_utils/dataclass_to_openapi.py b/marimo/_utils/dataclass_to_openapi.py
index c5ed729b88..27b426be19 100644
--- a/marimo/_utils/dataclass_to_openapi.py
+++ b/marimo/_utils/dataclass_to_openapi.py
@@ -228,13 +228,13 @@ class PythonTypeToOpenAPI:
def convert_dataclass(
self,
- cls: type[Any],
+ cls: Any,
processed_classes: dict[Any, str],
) -> dict[str, Any]:
"""Convert a dataclass to an OpenAPI schema.
Args:
- cls (Type[Any]): The dataclass to convert.
+ cls (Any): The dataclass type or instance to convert.
processed_classes (Dict[Any, str]): A dictionary of processed classes.
Raises:
@@ -243,13 +243,21 @@ class PythonTypeToOpenAPI:
Returns:
Dict[str, Any]: The OpenAPI schema.
"""
+ # If cls is an instance, get its class
+ if not isinstance(cls, type) and dataclasses.is_dataclass(cls):
+ cls = cls.__class__
+
if not dataclasses.is_dataclass(cls):
raise ValueError(f"{cls} is not a dataclass")
if cls in processed_classes:
return {"$ref": f"#/components/schemas/{processed_classes[cls]}"}
- schema_name = self.name_overrides.get(cls, cls.__name__)
+ # Handle both class and instance for __name__ access
+ if isinstance(cls, type):
+ schema_name = self.name_overrides.get(cls, cls.__name__)
+ else:
+ schema_name = self.name_overrides.get(cls, cls.__class__.__name__)
processed_classes[cls] = schema_name
type_hints = get_type_hints(cls)
diff --git a/pyproject.toml b/pyproject.toml
index 26bc6cc6d6..f31d6dd762 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -174,7 +174,7 @@ format = "ruff format"
python = "3.12"
installer = "uv"
dependencies = [
- "mypy~=1.10.1",
+ "mypy~=1.15.0",
# Types for mypy
"leafmap~=0.39.2",
"panel~=1.5.3",