type_check_file
Perform type checking on a Python file using Pyrefly's type inference engine to ensure code correctness and compliance with type annotations.
Instructions
Run type checking on a Python file using Pyrefly
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Path to the Python file to type check |
Implementation Reference
- Main execution logic for the type_check_file tool. Validates input, runs pyrefly check via helper, formats and returns results as text content.elif name == "type_check_file": file_path = arguments.get("file_path") if not file_path: raise ValueError("Missing file_path argument") if not os.path.exists(file_path): raise ValueError(f"File not found: {file_path}") result = await run_pyrefly_check(file_path) if result["success"]: return [types.TextContent( type="text", text=f"Type checking passed for {file_path}\n\nOutput:\n{result['output']}" )] else: return [types.TextContent( type="text", text=f"Type checking found issues in {file_path}\n\nErrors:\n{result['output']}\n{result.get('errors', '')}" )]
- src/mcp_pyrefly_autotype/server.py:385-398 (registration)Tool registration in list_tools() including name, description, and input schema definition.types.Tool( name="type_check_file", description="Run type checking on a Python file using Pyrefly", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Python file to type check" } }, "required": ["file_path"], }, ),
- Core helper function that runs the subprocess call to 'uv run pyrefly check' on the file and parses the result.async def run_pyrefly_check(file_path: str) -> Dict[str, Any]: """Run pyrefly type checking on a file.""" try: result = subprocess.run( ["uv", "run", "pyrefly", "check", file_path], capture_output=True, text=True, timeout=30 ) return { "success": result.returncode == 0, "output": result.stdout, "errors": result.stderr, "returncode": result.returncode } except subprocess.TimeoutExpired: return { "success": False, "error": "Pyrefly check execution timed out" } except Exception as e: return { "success": False, "error": str(e) }