refactor(src): Phase 10.2 batch 2 - outline_tool Result[T] migration
Migrates 3 sites in src/outline_tool.py: 1. L49 (outline body) - the ast.parse SyntaxError handler. outline() now returns Result[str]. On SyntaxError, the data is the formatted error string (preserved for backwards-compat with callers that read the formatted string), and the errors list has the ErrorInfo. 2. L90 (walk ast.unparse for returns) - was except ...: pass. Now appends ErrorInfo to enclosing parse_errors list. 3. L109 (walk ast.unparse for ImGui context) - same. outline() returns Result(data='\n'.join(output), errors=parse_errors). get_outline() also returns Result[str]. Tests updated to check result.ok and use result.data.
This commit is contained in:
+13
-10
@@ -34,12 +34,14 @@ import ast
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from src.result_types import Result, ErrorInfo, ErrorKind
|
||||
|
||||
|
||||
class CodeOutliner:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def outline(self, code: str) -> str:
|
||||
def outline(self, code: str) -> Result[str]:
|
||||
"""
|
||||
[C: tests/test_outline_tool.py:test_code_outliner_imgui_scopes, tests/test_outline_tool.py:test_code_outliner_nested_ifs, tests/test_outline_tool.py:test_code_outliner_type_hints]
|
||||
"""
|
||||
@@ -47,8 +49,9 @@ class CodeOutliner:
|
||||
try:
|
||||
tree = ast.parse(code)
|
||||
except SyntaxError as e:
|
||||
return f"ERROR parsing code: {e}"
|
||||
output = []
|
||||
return Result(data=f"ERROR parsing code: {e}", errors=[ErrorInfo(kind=ErrorKind.INVALID_INPUT, message=str(e), source="outline_tool.outline", original=e)])
|
||||
output: list[str] = []
|
||||
parse_errors: list[ErrorInfo] = []
|
||||
|
||||
def get_docstring(node: ast.AST) -> str | None:
|
||||
if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef, ast.ClassDef, ast.Module)):
|
||||
@@ -87,8 +90,8 @@ class CodeOutliner:
|
||||
if getattr(node, "returns", None):
|
||||
try:
|
||||
returns = f" -> {ast.unparse(node.returns)}"
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
except (ValueError, TypeError) as e:
|
||||
parse_errors.append(ErrorInfo(kind=ErrorKind.INTERNAL, message=f"ast.unparse failed for {node.name}.returns: {e}", source="outline_tool.walk", original=e))
|
||||
output.append(f"{' ' * indent}{prefix} {node.name}{returns} (Lines {start_line}-{end_line})")
|
||||
doc = get_docstring(node)
|
||||
if doc:
|
||||
@@ -106,8 +109,8 @@ class CodeOutliner:
|
||||
output.append(f"{' ' * indent}[ImGui Scope] {ctx_str} (Lines {start_line}-{end_line})")
|
||||
is_imgui = True
|
||||
break
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
pass
|
||||
except (ValueError, TypeError, AttributeError) as e:
|
||||
parse_errors.append(ErrorInfo(kind=ErrorKind.INTERNAL, message=f"ast.unparse failed for ImGui context: {e}", source="outline_tool.walk", original=e))
|
||||
for item in node.body:
|
||||
walk(item, indent + 1 if is_imgui else indent)
|
||||
else:
|
||||
@@ -119,12 +122,12 @@ class CodeOutliner:
|
||||
|
||||
for node in tree.body:
|
||||
walk(node)
|
||||
return "\n".join(output)
|
||||
return Result(data="\n".join(output), errors=parse_errors)
|
||||
|
||||
def get_outline(path: Path, code: str) -> str:
|
||||
def get_outline(path: Path, code: str) -> Result[str]:
|
||||
suffix = path.suffix.lower()
|
||||
if suffix == ".py":
|
||||
outliner = CodeOutliner()
|
||||
return outliner.outline(code)
|
||||
else:
|
||||
return f"Outlining not supported for {suffix} files yet."
|
||||
return Result(data=f"Outlining not supported for {suffix} files yet.")
|
||||
@@ -8,14 +8,15 @@ def test_code_outliner_type_hints():
|
||||
class Test:
|
||||
def my_method(self, x: int) -> str:
|
||||
return "hello"
|
||||
|
||||
|
||||
def my_func(a: bool) -> None:
|
||||
pass
|
||||
"""
|
||||
outliner = CodeOutliner()
|
||||
result = outliner.outline(code)
|
||||
assert "[Method] my_method -> str (Lines 3-4)" in result
|
||||
assert "[Func] my_func -> None (Lines 6-7)" in result
|
||||
assert result.ok, f"outline failed: {result.errors}"
|
||||
assert "[Method] my_method -> str (Lines 3-4)" in result.data
|
||||
assert "[Func] my_func -> None (Lines 6-7)" in result.data
|
||||
|
||||
def test_code_outliner_imgui_scopes():
|
||||
code = """
|
||||
@@ -27,8 +28,9 @@ def render_gui() -> None:
|
||||
"""
|
||||
outliner = CodeOutliner()
|
||||
result = outliner.outline(code)
|
||||
assert "[ImGui Scope] imscope.window('My Window') (Lines 3-6)" in result
|
||||
assert "[ImGui Scope] imscope.child('My Child') (Lines 5-6)" in result
|
||||
assert result.ok, f"outline failed: {result.errors}"
|
||||
assert "[ImGui Scope] imscope.window('My Window') (Lines 3-6)" in result.data
|
||||
assert "[ImGui Scope] imscope.child('My Child') (Lines 5-6)" in result.data
|
||||
|
||||
def test_code_outliner_nested_ifs():
|
||||
code = """
|
||||
@@ -39,4 +41,5 @@ def render_gui() -> None:
|
||||
"""
|
||||
outliner = CodeOutliner()
|
||||
result = outliner.outline(code)
|
||||
assert "[ImGui Scope] imscope.window('My Window') (Lines 4-5)" in result
|
||||
assert result.ok, f"outline failed: {result.errors}"
|
||||
assert "[ImGui Scope] imscope.window('My Window') (Lines 4-5)" in result.data
|
||||
|
||||
Reference in New Issue
Block a user