11# Copyright 2026 Marimo. All rights reserved.
22from __future__ import annotations
33
4+ import os
45import textwrap
6+ from pathlib import Path
57
6- from marimo ._ast .scanner import scan_notebook
8+ from marimo ._ast .load import get_notebook_status
9+ from marimo ._ast .parse import parse_notebook
10+ from marimo ._ast .scanner import scan_notebook , scan_parse_fallback
11+ from marimo ._lint .rule_engine import RuleEngine
12+ from marimo ._schemas .serialization import UnparsableCell
713
814
915class TestScanNotebook :
@@ -218,9 +224,6 @@ class TestScanParseIntegration:
218224
219225 @staticmethod
220226 def test_syntax_error_in_one_cell () -> None :
221- from marimo ._ast .parse import parse_notebook
222- from marimo ._schemas .serialization import UnparsableCell
223-
224227 source = textwrap .dedent ('''\
225228 import marimo
226229 __generated_with = "0.1.0"
@@ -257,9 +260,6 @@ def _():
257260
258261 @staticmethod
259262 def test_syntax_errors_in_all_cells () -> None :
260- from marimo ._ast .parse import parse_notebook
261- from marimo ._schemas .serialization import UnparsableCell
262-
263263 source = textwrap .dedent ('''\
264264 import marimo
265265 __generated_with = "0.1.0"
@@ -289,8 +289,6 @@ def test_non_marimo_file_syntax_error_does_not_raise() -> None:
289289 """A file without cell boundaries that has a syntax error should not
290290 raise — parse_notebook returns a best-effort result so watch/IPC
291291 are never broken by a syntax error."""
292- from marimo ._ast .parse import parse_notebook
293-
294292 source = '# Not a marimo file\n print("hello world\n '
295293 # Should not raise; returns a non-valid notebook
296294 result = parse_notebook (source )
@@ -299,8 +297,6 @@ def test_non_marimo_file_syntax_error_does_not_raise() -> None:
299297
300298 @staticmethod
301299 def test_valid_notebook_unchanged () -> None :
302- from marimo ._ast .parse import parse_notebook
303-
304300 source = textwrap .dedent ("""\
305301 import marimo
306302 __generated_with = "0.1.0"
@@ -327,8 +323,6 @@ def two(x):
327323
328324 @staticmethod
329325 def test_cell_names_preserved () -> None :
330- from marimo ._ast .parse import parse_notebook
331-
332326 source = textwrap .dedent ("""\
333327 import marimo
334328 __generated_with = "0.1.0"
@@ -356,9 +350,6 @@ def beta(x):
356350 def test_unparsable_cell_body_extraction () -> None :
357351 """Unparsable cells should contain only body code,
358352 not decorator/def/return."""
359- from marimo ._ast .parse import parse_notebook
360- from marimo ._schemas .serialization import UnparsableCell
361-
362353 source = textwrap .dedent ("""\
363354 import marimo
364355 __generated_with = "0.1.0"
@@ -385,9 +376,6 @@ def _():
385376 @staticmethod
386377 def test_unparsable_cell_empty_body () -> None :
387378 """A cell with only a broken return becomes empty."""
388- from marimo ._ast .parse import parse_notebook
389- from marimo ._schemas .serialization import UnparsableCell
390-
391379 source = textwrap .dedent ("""\
392380 import marimo
393381 __generated_with = "0.1.0"
@@ -409,9 +397,6 @@ def _():
409397 @staticmethod
410398 def test_unparsable_cell_multiline_signature () -> None :
411399 """Multi-line def signature is properly stripped."""
412- from marimo ._ast .parse import parse_notebook
413- from marimo ._schemas .serialization import UnparsableCell
414-
415400 source = textwrap .dedent ("""\
416401 import marimo
417402 __generated_with = "0.1.0"
@@ -436,9 +421,6 @@ def _(
436421 @staticmethod
437422 def test_unparsable_cell_decorator_with_args () -> None :
438423 """Decorator with arguments is properly stripped."""
439- from marimo ._ast .parse import parse_notebook
440- from marimo ._schemas .serialization import UnparsableCell
441-
442424 source = textwrap .dedent ("""\
443425 import marimo
444426 __generated_with = "0.1.0"
@@ -461,10 +443,6 @@ def _():
461443 @staticmethod
462444 def test_parse_error_in_notebook_file () -> None :
463445 """Test the actual _test_parse_error_in_notebook.py file."""
464- import os
465-
466- from marimo ._ast .load import get_notebook_status
467-
468446 filepath = os .path .join (
469447 os .path .dirname (os .path .realpath (__file__ )),
470448 "codegen_data/_test_parse_error_in_notebook.py" ,
@@ -482,11 +460,6 @@ def test_line_continuation_at_eof_file() -> None:
482460 The scanner should recover the cell as unparsable and find
483461 the run guard.
484462 """
485- import os
486-
487- from marimo ._ast .load import get_notebook_status
488- from marimo ._schemas .serialization import UnparsableCell
489-
490463 filepath = os .path .join (
491464 os .path .dirname (os .path .realpath (__file__ )),
492465 "codegen_data/_test_line_continuation_at_eof.py" ,
@@ -501,8 +474,6 @@ def test_line_continuation_at_eof_file() -> None:
501474 def test_scanner_generated_lines_typed () -> None :
502475 """scan_parse_fallback returns a frozenset of scanner-generated line
503476 numbers — no untyped AST attribute is used."""
504- from marimo ._ast .scanner import scan_parse_fallback
505-
506477 source = textwrap .dedent ("""\
507478 import marimo
508479 __generated_with = "0.1.0"
@@ -535,8 +506,6 @@ def broken():
535506 def test_scanner_generated_lines_existing_unparsable_not_flagged () -> None :
536507 """Pre-existing app._unparsable_cell() in source must NOT appear in
537508 scanner_generated_lines — only cells the scanner itself created."""
538- from marimo ._ast .scanner import scan_parse_fallback
539-
540509 # This source parses fine (ast.parse succeeds), so scan_parse_fallback
541510 # returns ([], frozenset()) — no scanner-generated lines.
542511 source = textwrap .dedent ("""\
@@ -562,10 +531,6 @@ def test_scanner_generated_lines_existing_unparsable_not_flagged() -> None:
562531 def test_line_continuation_no_duplicate_diagnostics () -> None :
563532 """Scanner-generated unparsable cells should produce only
564533 one diagnostic (MB001), not a duplicate from MF001."""
565- from marimo ._ast .parse import parse_notebook
566- from marimo ._lint .rule_engine import RuleEngine
567- from marimo ._schemas .serialization import UnparsableCell
568-
569534 source = textwrap .dedent ("""\
570535 import marimo
571536 __generated_with = "0.1.0"
@@ -605,10 +570,6 @@ def test_encoding_error_file(tmp_path: object) -> None:
605570 The file declares ASCII encoding but has a Latin-1 byte (0xe9).
606571 Should load gracefully with errors, not crash.
607572 """
608- from pathlib import Path
609-
610- from marimo ._ast .load import get_notebook_status
611-
612573 tmp = Path (str (tmp_path ))
613574 filepath = tmp / "encoding_errors.py"
614575 # Write raw bytes: ASCII encoding declaration + Latin-1 byte 0xe9
0 commit comments