Extract the body of a wrapped `def _():` cell after ruff formatting. Used by RuffFormatter to unwrap cells that were temporarily wrapped in a dummy function so ruff applies function-scope blank-line rules (E301) instead of file-scope rules (E302).
(formatted: str)
| 85 | |
| 86 | |
| 87 | def unwrap_cell_body(formatted: str) -> str: |
| 88 | """Extract the body of a wrapped `def _():` cell after ruff formatting. |
| 89 | |
| 90 | Used by RuffFormatter to unwrap cells that were temporarily wrapped in a |
| 91 | dummy function so ruff applies function-scope blank-line rules (E301) |
| 92 | instead of file-scope rules (E302). |
| 93 | """ |
| 94 | tree = ast_parse(formatted) |
| 95 | fn = tree.body[0] |
| 96 | if not isinstance(fn, ast.FunctionDef): |
| 97 | raise ValueError( |
| 98 | f"Expected a FunctionDef node, got {type(fn).__name__}" |
| 99 | ) |
| 100 | if fn.end_lineno is None: |
| 101 | raise ValueError("FunctionDef node has no end_lineno") |
| 102 | extractor = Extractor(formatted) |
| 103 | raw = extractor.extract_from_offsets( |
| 104 | fn.body[0].lineno - 1, 0, fn.end_lineno - 1, fn.end_col_offset |
| 105 | ) |
| 106 | return fixed_dedent(raw).strip() |
| 107 | |
| 108 | |
| 109 | def extract_lineno(node: Node) -> int: |
no test coverage detected
searching dependent graphs…