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: flatten subtype (#1221)

Akshay Agrawal32c2ee13716b
diff --git a/marimo/_utils/flatten.py b/marimo/_utils/flatten.py
index 8d4dfc006c..841330fe45 100644
--- a/marimo/_utils/flatten.py
+++ b/marimo/_utils/flatten.py
@@ -12,7 +12,7 @@ installation of dm-tree on macOS is buggy
from __future__ import annotations
import itertools
-from typing import Any, Callable, Dict, List, Tuple, Union
+from typing import Any, Callable, Dict, List, Tuple, Type, Union
STRUCT_TYPE = Union[Tuple[Any, ...], List[Any], Dict[Any, Any]]
UNFLATTEN_TYPE = Callable[[List[Any]], Union[STRUCT_TYPE, Any]]
@@ -31,6 +31,14 @@ def _flatten_sequence(
value: list[Any] | tuple[Any, ...], json_compat_keys: bool, seen: set[int]
) -> FLATTEN_RET_TYPE:
"""Flatten a sequence of values"""
+ base_type: Type[List[Any]] | Type[Tuple[Any, ...]]
+ if isinstance(value, list):
+ base_type = list
+ elif isinstance(value, tuple):
+ base_type = tuple
+ else:
+ raise ValueError("value is not a list or tuple: ", value)
+
# Algorithm:
#
# Accumulate a list of flattened pieces and unflattener functions,
@@ -47,7 +55,7 @@ def _flatten_sequence(
# the recursion stack depth compared to the reference implementation
if not value:
# unflattener returns an empty tuple or empty list
- return [], lambda _: type(value)()
+ return [], lambda _: base_type()
lengths = []
flattened_pieces: list[list[Any]] = []
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index 404e71d5a4..dd0df659d0 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -348,8 +348,7 @@ class TestApp:
@app.cell
def __() -> tuple[Any]:
- def foo() -> None:
- ...
+ def foo() -> None: ...
return (foo,)
diff --git a/tests/_smoke_tests/test_run_as_script.py b/tests/_smoke_tests/test_run_as_script.py
index 734a85102c..1fa096daf6 100644
--- a/tests/_smoke_tests/test_run_as_script.py
+++ b/tests/_smoke_tests/test_run_as_script.py
@@ -6,6 +6,7 @@ import sys
from typing import TYPE_CHECKING
import pytest
+
from marimo._dependencies.dependencies import DependencyManager
if TYPE_CHECKING:
@@ -13,7 +14,9 @@ if TYPE_CHECKING:
class TestRunTutorialsAsScripts:
- def assert_not_errored(self, p: subprocess.CompletedProcess[bytes]) -> None:
+ def assert_not_errored(
+ self, p: subprocess.CompletedProcess[bytes]
+ ) -> None:
assert p.returncode == 0
assert not any(
line.startswith("Traceback")
diff --git a/tests/_utils/test_flatten.py b/tests/_utils/test_flatten.py
index 63a240423c..d3fff5ff52 100644
--- a/tests/_utils/test_flatten.py
+++ b/tests/_utils/test_flatten.py
@@ -189,3 +189,15 @@ def test_flatten_repeated_structure_does_not_raise() -> None:
y = [x, x, x]
# should not raise
flatten(y)
+
+
+def test_flatten_custom_list() -> None:
+ class CustomList(list):
+ def __init__(self, extra_arg):
+ del extra_arg
+ super().__init__(())
+
+ custom_list = CustomList("data")
+ v, u = flatten(custom_list)
+ assert v == []
+ assert u(v) == []