2424 has_marimo_in_script_metadata ,
2525 is_marimo_dependency ,
2626)
27- from marimo ._utils .scripts import with_python_version_requirement
2827from marimo ._utils .uv import find_uv_bin
2928from marimo ._utils .versions import is_editable
3029from marimo ._version import __version__
@@ -324,14 +323,12 @@ def construct_uv_command(
324323def _ensure_python_version_in_script_metadata (name : str ) -> None :
325324 """Add requires-python to script metadata if not present.
326325
327- Reads the file, parses the PEP 723 metadata, adds requires-python
328- if missing, and writes the updated metadata back .
326+ Inserts a requires-python line directly into the existing PEP 723
327+ metadata block without re-serializing, to avoid reformatting diffs .
329328 """
330- from marimo ._utils .scripts import (
331- REGEX ,
332- read_pyproject_from_script ,
333- write_pyproject_to_script ,
334- )
329+ import re
330+
331+ from marimo ._utils .scripts import read_pyproject_from_script
335332
336333 with open (name , encoding = "utf-8" ) as f :
337334 content = f .read ()
@@ -342,17 +339,22 @@ def _ensure_python_version_in_script_metadata(name: str) -> None:
342339 return
343340
344341 if "requires-python" in project :
345- # Already has Python version
346342 return
347343
348- # Generate new script metadata block
349- project = with_python_version_requirement (project )
350- new_block = write_pyproject_to_script (project )
351-
352- # Replace the old block with the new one
353- import re
344+ version_tuple = platform .python_version_tuple ()
345+ requires_line = (
346+ f'# requires-python = ">={ version_tuple [0 ]} .{ version_tuple [1 ]} "\n '
347+ )
354348
355- new_content = re .sub (REGEX , new_block , content , count = 1 )
349+ # Insert directly after the opening "# /// script" marker to avoid
350+ # re-serializing the entire block and causing formatting churn.
351+ new_content = re .sub (
352+ r"^# /// script$" ,
353+ "# /// script\n " + requires_line .rstrip (),
354+ content ,
355+ count = 1 ,
356+ flags = re .MULTILINE ,
357+ )
356358
357359 if new_content != content :
358360 with open (name , "w" , encoding = "utf-8" ) as f :
0 commit comments