MCPcopy
hub / github.com/marimo-team/marimo / _extract_body_code

Function _extract_body_code

marimo/_ast/scanner.py:397–421  ·  view source on GitHub ↗

Extract just the body code from a cell source string. Strips decorator lines, def/class/with header, and trailing return. Used when a cell fails ast.parse() to get the body for UnparsableCell.

(cell_source: str, kind: str)

Source from the content-addressed store, hash-verified

395
396
397def _extract_body_code(cell_source: str, kind: str) -> str:
398 """Extract just the body code from a cell source string.
399
400 Strips decorator lines, def/class/with header, and trailing return.
401 Used when a cell fails ast.parse() to get the body for UnparsableCell.
402 """
403 from marimo._ast.parse import fixed_dedent
404
405 if kind == "unparsable":
406 # Already app._unparsable_cell(...) — return as-is
407 return cell_source
408
409 lines = cell_source.splitlines(keepends=True)
410 body_start = _find_body_start(lines, kind)
411
412 if body_start >= len(lines):
413 return ""
414
415 body_lines = list(lines[body_start:])
416
417 # Strip trailing return at body indentation level
418 _strip_trailing_return(body_lines)
419
420 body = "".join(body_lines)
421 return fixed_dedent(body).strip()
422
423
424def _find_body_start(lines: list[str], kind: str) -> int:

Callers 1

scan_parse_fallbackFunction · 0.85

Calls 5

fixed_dedentFunction · 0.90
_find_body_startFunction · 0.85
_strip_trailing_returnFunction · 0.85
stripMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…