Skip to content

Commit 8144711

Browse files
committed
fix: --no-fix
1 parent 036e19f commit 8144711

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

marimo/_cli/convert/commands.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@
2424
"If not provided, the converted notebook will be printed to stdout."
2525
),
2626
)
27+
@click.option(
28+
"--no-fix",
29+
type=bool,
30+
default=False,
31+
help=(
32+
"If set, do not attempt to fix common issues in the converted "
33+
"notebook. Only applicable when converting from .ipynb files."
34+
),
35+
hidden=True,
36+
)
2737
def convert(
2838
filename: str,
2939
output: Optional[Path],
40+
no_fix: bool = False,
3041
) -> None:
3142
r"""Convert a Jupyter notebook, Markdown file, or Python script to a marimo notebook.
3243
@@ -79,7 +90,7 @@ def convert(
7990

8091
text = load_external_file(filename, ext)
8192
if ext == ".ipynb":
82-
notebook = MarimoConvert.from_ipynb(text).to_py()
93+
notebook = MarimoConvert.from_ipynb(text, fix=not no_fix).to_py()
8394
elif ext in (".md", ".qmd"):
8495
notebook = MarimoConvert.from_md(text).to_py()
8596
else:

marimo/_convert/converters.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,19 @@ def from_md(source: str) -> MarimoConverterIntermediate:
115115
return MarimoConvert.from_ir(convert_from_md_to_marimo_ir(source))
116116

117117
@staticmethod
118-
def from_ipynb(source: str) -> MarimoConverterIntermediate:
118+
def from_ipynb(
119+
source: str, fix: bool = True
120+
) -> MarimoConverterIntermediate:
119121
"""Convert from Jupyter notebook JSON.
120122
121123
Args:
122124
source: Jupyter notebook JSON string
123125
"""
124126
from marimo._convert.ipynb import convert_from_ipynb_to_notebook_ir
125127

126-
return MarimoConvert.from_ir(convert_from_ipynb_to_notebook_ir(source))
128+
return MarimoConvert.from_ir(
129+
convert_from_ipynb_to_notebook_ir(source, fix=fix)
130+
)
127131

128132
@staticmethod
129133
def from_notebook_v1(

marimo/_convert/ipynb.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from marimo._ast.transformers import NameTransformer, RemoveImportTransformer
1515
from marimo._ast.variables import is_local
1616
from marimo._ast.visitor import Block, NamedNode, ScopedVisitor
17+
from marimo._convert.comment_preserver import CommentPreserver
1718
from marimo._convert.utils import markdown_to_marimo
1819
from marimo._runtime.dataflow import DirectedGraph
1920
from marimo._schemas.serialization import (
@@ -709,7 +710,10 @@ def extract_inline_meta(script: str) -> tuple[str | None, str]:
709710

710711

711712
def _transform_sources(
712-
sources: list[str], metadata: list[dict[str, Any]], hide_flags: list[bool]
713+
sources: list[str],
714+
metadata: list[dict[str, Any]],
715+
hide_flags: list[bool],
716+
fix: bool = True,
713717
) -> list[CodeCell]:
714718
"""
715719
Process raw sources and metadata into finalized cells.
@@ -721,7 +725,6 @@ def _transform_sources(
721725
722726
After this step, cells are ready for execution or rendering.
723727
"""
724-
from marimo._convert.comment_preserver import CommentPreserver
725728

726729
# Define transforms that don't need comment preservation
727730
simple_transforms = [
@@ -730,13 +733,6 @@ def _transform_sources(
730733
transform_exclamation_mark,
731734
]
732735

733-
# Define transforms that should preserve comments
734-
comment_preserving_transforms = [
735-
transform_remove_duplicate_imports,
736-
transform_fixup_multiple_definitions,
737-
transform_duplicate_definitions,
738-
]
739-
740736
# Run simple transforms first (no comment preservation needed)
741737
for source_transform in simple_transforms:
742738
new_sources = source_transform(sources)
@@ -745,6 +741,16 @@ def _transform_sources(
745741
)
746742
sources = new_sources
747743

744+
if not fix:
745+
return bind_cell_metadata(sources, metadata, hide_flags)
746+
747+
# Define transforms that should preserve comments
748+
comment_preserving_transforms = [
749+
transform_remove_duplicate_imports,
750+
transform_fixup_multiple_definitions,
751+
transform_duplicate_definitions,
752+
]
753+
748754
# Create comment preserver from the simplified sources
749755
comment_preserver = CommentPreserver(sources)
750756

@@ -773,6 +779,7 @@ def _transform_sources(
773779

774780
def convert_from_ipynb_to_notebook_ir(
775781
raw_notebook: str,
782+
fix: bool = True,
776783
) -> NotebookSerializationV1:
777784
"""
778785
Convert a raw notebook to a NotebookSerializationV1 object.
@@ -801,7 +808,9 @@ def convert_from_ipynb_to_notebook_ir(
801808
metadata.append(cell.get("metadata", {}))
802809
hide_flags.append(is_markdown)
803810

804-
transformed_cells = _transform_sources(sources, metadata, hide_flags)
811+
transformed_cells = _transform_sources(
812+
sources, metadata, hide_flags, fix=fix
813+
)
805814

806815
return NotebookSerializationV1(
807816
app=AppInstantiation(),

0 commit comments

Comments
 (0)