Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Compile docs snippets and attribute mypy diagnostics to resolved SDK symbols. |
| 2 | |
| 3 | The TypeScript driver passes a request file containing workDir, sdkPython, |
| 4 | syntaxOnly, and snippets ({id, code, typeCheck}). No snippet is executed. Results |
| 5 | separate SDK API diagnostics from other mypy diagnostics; line numbers are |
| 6 | snippet-relative, with the prelude removed. The pinned mypy build API supplies |
| 7 | resolved expression types, so local aliases, nested scopes, and same-named |
| 8 | third-party classes cannot change SDK attribution. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import ast |
| 14 | import io |
| 15 | import json |
| 16 | import os |
| 17 | import re |
| 18 | import symtable |
| 19 | import sys |
| 20 | import tokenize |
| 21 | from pathlib import Path |
| 22 | |
| 23 | HERE = Path(__file__).resolve().parent |
| 24 | PYTHON_ROOT = HERE.parents[2] / "python" |
| 25 | PRELUDE = HERE / "prelude.py" |
| 26 | # Some diagnostics, such as unused-ignore, have no column; they must not be dropped. |
| 27 | MYPY_LINE = re.compile(r'^(\w+)\.py:(\d+)(?::(\d+))?: (error|note): (.*?)(?: \[([\w-]+)\])?$') |
| 28 | CALL_CODES = {"call-arg", "call-overload", "arg-type", "typeddict-item", "typeddict-unknown-key"} |
| 29 | ATTRIBUTE_CODES = {"attr-defined", "union-attr"} |
| 30 | IMPORT_CODES = {"import-not-found", "import-untyped"} |
| 31 | # Value misuse whose owner is an expression type: indexing an SDK model and |
| 32 | # assigning an SDK result to an incompatible annotation. |
| 33 | VALUE_CODES = {"index", "assignment"} |
| 34 | TYPE_IGNORE = re.compile(r"#\s*type:\s*ignore\b(\[[^\]]*\])?") |
| 35 | |
| 36 | |
| 37 | def bound_names(code: str) -> set[str]: |
| 38 | """Module bindings only: a function parameter/local cannot hide the prelude. |
| 39 | |
| 40 | Python's symbol table also handles comprehensions, pattern bindings, and |
| 41 | global declarations without treating nested lexical scopes as module scope. |
| 42 | """ |
| 43 | return { |
| 44 | symbol.get_name() |
| 45 | for symbol in symtable.symtable(code, "snippet.py", "exec").get_symbols() |
| 46 | if symbol.is_assigned() or symbol.is_imported() or symbol.is_namespace() |
| 47 | } |
| 48 | |
| 49 | |
| 50 | def prelude_bindings(source: str) -> list[tuple[int, str | None]]: |
| 51 | """(0-based line, public name it binds); None means always keep the line.""" |
| 52 | lines = source.splitlines() |
| 53 | bindings: list[tuple[int, str | None]] = [(i, None) for i in range(len(lines))] |
| 54 | for stmt in ast.parse(source).body: |
| 55 | name: str | None = None |
| 56 | if isinstance(stmt, ast.AnnAssign) and isinstance(stmt.target, ast.Name): |
| 57 | name = stmt.target.id |
| 58 | elif isinstance(stmt, (ast.Import, ast.ImportFrom)) and len(stmt.names) == 1: |
| 59 | alias = stmt.names[0] |
| 60 | name = alias.asname or alias.name.split(".")[0] |
| 61 | if name is None or name.startswith("_"): |
| 62 | continue |
| 63 | if stmt.end_lineno != stmt.lineno: |
| 64 | raise SystemExit(f"prelude.py:{stmt.lineno}: bindings must fit on one line") |
| 65 | bindings[stmt.lineno - 1] = (stmt.lineno - 1, name) |
| 66 | return bindings |
| 67 | |
| 68 | |
| 69 | def module_source(prelude_lines: list[str], bindings: list[tuple[int, str | None]], snippet: ast.Module, code: str) -> str: |
| 70 | """Blank shadowed bindings without changing diagnostic line offsets.""" |
| 71 | bound = bound_names(code) |
| 72 | kept = ["" if name in bound else prelude_lines[i] for i, name in bindings] |
| 73 | # Future imports must still precede the prelude's normal imports. Reuse its |
| 74 | # first comment line and blank their original lines, preserving every offset. |
| 75 | futures = [stmt for stmt in snippet.body if isinstance(stmt, ast.ImportFrom) and stmt.module == "__future__"] |
| 76 | code_lines = code.splitlines() |
| 77 | if futures: |
| 78 | if not kept[0].startswith("#"): |
| 79 | raise SystemExit("prelude.py must start with a comment reserved for future imports") |
| 80 | kept[0] = "; ".join(ast.unparse(stmt) for stmt in futures) |
| 81 | for stmt in futures: |
| 82 | for i in range(stmt.lineno - 1, stmt.end_lineno): |
| 83 | code_lines[i] = "" |
| 84 | return "\n".join([*kept, *code_lines]) + "\n" |
| 85 | |
| 86 | |
| 87 | def provider_packages() -> list[Path]: |
| 88 | return sorted((PYTHON_ROOT / "providers").glob("*/composio_*")) |
| 89 | |
| 90 | |
| 91 | def run_mypy(files: list[str], request: dict, trees: dict[str, ast.Module], prelude_lines: int) -> dict[str, list]: |
| 92 | from mypy.build import build |
| 93 | from mypy.errors import CompileError |
| 94 | from mypy.inspections import find_all_by_location |
| 95 | from mypy.main import process_options |
| 96 | from mypy.nodes import CallExpr, IndexExpr, MemberExpr, MypyFile, RefExpr, Var |
| 97 | from mypy.types import CallableType, Instance, Overloaded, TypedDictType, TypeType, UnionType, get_proper_type |
| 98 | |
| 99 | packages = {"composio", "composio_client", *(path.name for path in provider_packages())} |
| 100 | |
| 101 | def sdk_name(fullname: str) -> bool: |
| 102 | return fullname.split(".")[0] in packages |
| 103 | |
| 104 | def sdk_type(typ) -> bool: |
| 105 | typ = get_proper_type(typ) |
| 106 | if isinstance(typ, Instance): |
| 107 | return sdk_name(typ.type.fullname) |
| 108 | if isinstance(typ, CallableType): |
| 109 | definition = typ.definition |
| 110 | return bool(definition and sdk_name(definition.fullname)) or ( |
| 111 | typ.is_type_obj() and sdk_name(typ.type_object().fullname) |
| 112 | ) |
| 113 | if isinstance(typ, (UnionType, Overloaded)): |
| 114 | return any(sdk_type(item) for item in typ.items) |
| 115 | if isinstance(typ, TypeType): |
| 116 | return sdk_type(typ.item) |
| 117 | if isinstance(typ, TypedDictType): |
| 118 | return sdk_type(typ.fallback) |
| 119 | return False |
| 120 | |
| 121 | # Editable installs use an import hook mypy cannot follow. Resolve the SDK |
| 122 | # and ALL local provider packages from source, not a published SDK wheel. |
| 123 | os.environ["MYPYPATH"] = os.pathsep.join([str(PYTHON_ROOT), *(str(path.parent) for path in provider_packages())]) |
| 124 | sources, options = process_options([ |
| 125 | "--config-file=", |
| 126 | "--python-version=3.12", |
| 127 | f"--python-executable={request['sdkPython']}", |
| 128 | "--follow-imports=silent", |
| 129 | "--check-untyped-defs", |
| 130 | "--allow-redefinition", |
| 131 | # A documented typing gap must still be real: stale ignores fail. |
| 132 | "--warn-unused-ignores", |
| 133 | "--show-error-codes", |
| 134 | "--show-column-numbers", |
| 135 | "--hide-error-context", |
| 136 | "--no-pretty", |
| 137 | "--no-color-output", |
| 138 | "--no-error-summary", |
| 139 | # Cached modules don't retain expression types. Attribution must not |
| 140 | # disappear on a warm cache (nor depend on another developer's cache). |
| 141 | "--no-incremental", |
| 142 | *files, |
| 143 | ]) |
| 144 | options.export_types = True |
| 145 | options.preserve_asts = True |
| 146 | try: |
| 147 | result = build(sources, options) |
| 148 | except CompileError as error: |
| 149 | raise SystemExit("mypy failed:\n" + "\n".join(error.messages)) from error |
| 150 | |
| 151 | def sdk_reference(expression) -> bool: |
| 152 | if not isinstance(expression, RefExpr) or expression.node is None: |
| 153 | return False |
| 154 | node = expression.node |
| 155 | if isinstance(node, MypyFile): |
| 156 | return sdk_name(node.fullname) |
| 157 | # Mypy gives local parameters bare fullnames, e.g. an int parameter |
| 158 | # named "composio". Only a resolved module may own a package-only name; |
| 159 | # other SDK symbols must have a qualified definition inside the package. |
| 160 | return "." in node.fullname and sdk_name(node.fullname) |
| 161 | |
| 162 | def sdk_expression(expression) -> bool: |
| 163 | if sdk_reference(expression): |
| 164 | return True |
| 165 | if sdk_type(result.types.get(expression)): |
| 166 | return True |
| 167 | if isinstance(expression, MemberExpr): |
| 168 | return sdk_type(result.types.get(expression.expr)) or sdk_reference(expression.expr) |
| 169 | return False |
| 170 | |
| 171 | def sdk_import(snippet_id: str, line: int) -> bool: |
| 172 | for node in ast.walk(trees[snippet_id]): |
| 173 | if not isinstance(node, (ast.Import, ast.ImportFrom)) or not node.lineno <= line <= node.end_lineno: |
| 174 | continue |
| 175 | modules = [node.module or ""] if isinstance(node, ast.ImportFrom) else [alias.name for alias in node.names] |
| 176 | return any(sdk_name(module) for module in modules) |
| 177 | return False |
| 178 | |
| 179 | def sdk_symbol(symbol) -> bool: |
| 180 | node = symbol.node if symbol is not None else None |
| 181 | if isinstance(node, Var): |
| 182 | return sdk_type(node.type) |
| 183 | if isinstance(node, MypyFile): |
| 184 | return sdk_name(node.fullname) |
| 185 | return node is not None and "." in node.fullname and sdk_name(node.fullname) |
| 186 | |
| 187 | api: list[dict] = [] |
| 188 | other: list[dict] = [] |
| 189 | directives: list[dict] = [] |
| 190 | for text in result.errors: |
| 191 | match = MYPY_LINE.match(text) |
| 192 | if not match: |
| 193 | continue |
| 194 | snippet_id, line_text, column_text, severity, message, code = match.groups() |
| 195 | if severity != "error" or snippet_id not in trees: |
| 196 | continue |
| 197 | line, column = int(line_text), int(column_text or 1) - 1 |
| 198 | snippet_line = line - prelude_lines |
| 199 | if snippet_line < 1: |
| 200 | raise SystemExit(f"Python snippet prelude failed: {text}") |
| 201 | sdk = False |
| 202 | if code == "unused-ignore": |
| 203 | directives.append({"id": snippet_id, "line": snippet_line, "message": f"{message} [{code}]"}) |
| 204 | continue |
| 205 | if code in IMPORT_CODES or (code == "attr-defined" and sdk_import(snippet_id, snippet_line)): |
| 206 | sdk = sdk_import(snippet_id, snippet_line) |
| 207 | elif code in ATTRIBUTE_CODES | CALL_CODES | VALUE_CODES: |
| 208 | expressions = find_all_by_location(result.files[snippet_id], line, column) |
| 209 | if code in ATTRIBUTE_CODES: |
| 210 | attribute = re.search(r'has no attribute "([^"]+)"', message) |
| 211 | owner = next(( |
| 212 | expr for expr in expressions |
| 213 | if isinstance(expr, MemberExpr) and attribute and expr.name == attribute[1] |
| 214 | ), None) |
| 215 | # A diagnostic on schema.input_parameters["properties"].pop() |
| 216 | # starts at schema, just like the inner SDK member access. The |
| 217 | # missing member's own receiver (object here), not an unrelated |
| 218 | # inner SDK-valued expression, determines its provenance. |
| 219 | sdk = owner is not None and sdk_expression(owner) |
| 220 | elif code == "index": |
| 221 | # `response["status"]` on an SDK model: the indexed receiver, |
| 222 | # not the index value or the enclosing call, owns the error. |
| 223 | subscript = next((expr for expr in expressions if isinstance(expr, IndexExpr)), None) |
| 224 | sdk = subscript is not None and sdk_expression(subscript.base) |
| 225 | elif code == "assignment": |
| 226 | # Mypy reports assignments at the rvalue; the outermost |
| 227 | # expression at that position is the assigned value. |
| 228 | sdk = bool(expressions) and ( |
| 229 | sdk_type(result.types.get(expressions[-1])) |
| 230 | or (isinstance(expressions[-1], CallExpr) and sdk_expression(expressions[-1].callee)) |
| 231 | ) |
| 232 | else: |
| 233 | call = next((expr for expr in expressions if isinstance(expr, CallExpr)), None) |
| 234 | sdk = call is not None and sdk_expression(call.callee) |
| 235 | if code.startswith("typeddict-"): |
| 236 | # A missing key on an SDK TypedDict belongs to the indexed |
| 237 | # receiver even when it is an argument to a non-SDK call. |
| 238 | sdk = sdk or any( |
| 239 | isinstance(expr, IndexExpr) and sdk_type(result.types.get(expr.base)) |
| 240 | for expr in expressions |
| 241 | ) or (call is None and any(sdk_type(result.types.get(expr)) for expr in expressions)) |
| 242 | elif code == "name-defined": |
| 243 | # An undefined name is fragment context, unless mypy identifies it |
| 244 | # as a misspelling of a binding that holds an SDK object. |
| 245 | sdk = any( |
| 246 | sdk_symbol(result.files[snippet_id].names.get(name)) |
| 247 | for name in re.findall(r'"([^"]+)"', message.partition("did you mean")[2]) |
| 248 | ) |
| 249 | diagnostic = {"id": snippet_id, "line": snippet_line, "message": f"{message} [{code}]"} |
| 250 | (api if sdk else other).append(diagnostic) |
| 251 | return {"api": api, "other": other, "directives": directives, "mypy": result.errors} |
| 252 | |
| 253 | |
| 254 | def ignore_directives(snippet_id: str, code: str, tree: ast.Module) -> list[dict]: |
| 255 | """A `# type: ignore` must name its error codes and carry an explanation. |
| 256 | |
| 257 | The explanation may follow the ignore on the same line, sit inside the |
| 258 | enclosing statement, or be the comment line directly above it. This |
| 259 | mirrors the TypeScript gate's rejection of blanket suppressions. |
| 260 | """ |
| 261 | comments = { |
| 262 | token.start[0]: token.string |
| 263 | for token in tokenize.generate_tokens(io.StringIO(code).readline) |
| 264 | if token.type == tokenize.COMMENT |
| 265 | } |
| 266 | lines = code.splitlines() |
| 267 | statements = [node for node in ast.walk(tree) if isinstance(node, ast.stmt)] |
| 268 | directives: list[dict] = [] |
| 269 | for line, comment in comments.items(): |
| 270 | match = TYPE_IGNORE.search(comment) |
| 271 | if not match: |
| 272 | continue |
| 273 | if not match[1] or not match[1].strip("[] "): |
| 274 | directives.append({"id": snippet_id, "line": line, "message": ( |
| 275 | "`# type: ignore` must name the suppressed mypy error codes, e.g. `# type: ignore[arg-type]`" |
| 276 | )}) |
| 277 | continue |
| 278 | enclosing = min( |
| 279 | (stmt for stmt in statements if stmt.lineno <= line <= (stmt.end_lineno or stmt.lineno)), |
| 280 | key=lambda stmt: (stmt.end_lineno or stmt.lineno) - stmt.lineno, |
| 281 | default=None, |
| 282 | ) |
| 283 | start, end = (enclosing.lineno, enclosing.end_lineno or enclosing.lineno) if enclosing else (line, line) |
| 284 | explained = ( |
| 285 | bool(comment[match.end():].strip(" #")) |
| 286 | or any(other != line and start <= other <= end for other in comments) |
| 287 | or (start - 1 in comments and lines[start - 2].lstrip().startswith("#")) |
| 288 | ) |
| 289 | if not explained: |
| 290 | directives.append({"id": snippet_id, "line": line, "message": ( |
| 291 | "`# type: ignore` needs a comment explaining the verified SDK typing gap" |
| 292 | )}) |
| 293 | return directives |
| 294 | |
| 295 | |
| 296 | def main() -> None: |
| 297 | request = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) |
| 298 | work_dir = Path(request["workDir"]) |
| 299 | syntax: list[dict] = [] |
| 300 | parsed: list[tuple[str, str, ast.Module]] = [] |
| 301 | for snippet in request["snippets"]: |
| 302 | name = f"{snippet['id']}.py" |
| 303 | try: |
| 304 | compile(snippet["code"], name, "exec", flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT, dont_inherit=True) |
| 305 | tree = ast.parse(snippet["code"], name) |
| 306 | except SyntaxError as error: |
| 307 | syntax.append({"id": snippet["id"], "line": error.lineno or 1, "message": error.msg}) |
| 308 | continue |
| 309 | if snippet["typeCheck"]: |
| 310 | parsed.append((snippet["id"], snippet["code"], tree)) |
| 311 | |
| 312 | prelude_source = PRELUDE.read_text(encoding="utf-8") |
| 313 | prelude_lines = prelude_source.splitlines() |
| 314 | bindings = prelude_bindings(prelude_source) |
| 315 | output: dict[str, list] = {"syntax": syntax, "api": [], "other": [], "directives": [], "mypy": []} |
| 316 | if not request["syntaxOnly"] and parsed: |
| 317 | files: list[str] = [] |
| 318 | for snippet_id, code, tree in parsed: |
| 319 | path = work_dir / f"{snippet_id}.py" |
| 320 | path.write_text(module_source(prelude_lines, bindings, tree, code), encoding="utf-8") |
| 321 | files.append(path.name) |
| 322 | os.chdir(work_dir) |
| 323 | output.update(run_mypy(files, request, {name: tree for name, _, tree in parsed}, len(prelude_lines))) |
| 324 | output["directives"] += [ |
| 325 | directive for snippet_id, code, tree in parsed for directive in ignore_directives(snippet_id, code, tree) |
| 326 | ] |
| 327 | json.dump(output, sys.stdout) |
| 328 | |
| 329 | |
| 330 | if __name__ == "__main__": |
| 331 | main() |