Spaces:
Sleeping
Sleeping
File size: 4,288 Bytes
afb9b09 658c35a afb9b09 658c35a afb9b09 658c35a afb9b09 658c35a afb9b09 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | # tools/func_diff.py
import inspect
import importlib
import importlib.metadata
from typing import Any
def get_package_version(lib: str) -> str:
"""Get installed version without importing the library."""
try:
return importlib.metadata.version(lib)
except importlib.metadata.PackageNotFoundError:
return "not installed"
def get_func_info(lib: str, func_path: str) -> dict[str, Any]:
"""Get signature and docstring for a function in an installed library."""
try:
mod = importlib.import_module(lib)
obj = mod
for part in func_path.split("."):
obj = getattr(obj, part)
return {
"lib": lib,
"func": func_path,
"signature": str(inspect.signature(obj)),
"docstring": inspect.getdoc(obj) or "No docstring available",
"version": get_package_version(lib),
"source": "inspect",
"found": True
}
except ImportError:
return {
"lib": lib,
"func": func_path,
"signature": None,
"docstring": None,
"version": "not installed",
"source": "not_found",
"found": False,
"error": f"'{lib}' is not installed β call pip_install('{lib}') first"
}
except AttributeError:
return {
"lib": lib,
"func": func_path,
"signature": None,
"docstring": None,
"version": get_package_version(lib),
"source": "not_found",
"found": False,
"error": f"'{lib}' is installed but '{func_path}' not found β check mappings.py"
}
def _strip_lib_prefix(lib: str, func_path: str) -> str:
"""
Strip the library prefix from a function path if the agent included it.
e.g. "httpx.get" with lib="httpx" β "get"
"httpx.Client.get" with lib="httpx" β "Client.get"
"nn.Linear" with lib="torch" β "nn.Linear" (no prefix match)
"""
prefix = lib + "."
if func_path.startswith(prefix):
return func_path[len(prefix):]
return func_path
def get_func_diff(old_lib: str, new_lib: str, func_path: str, new_func_path: str | None = None) -> dict[str, Any]:
"""
Compare a function between old and new library.
new_func_path handles renames e.g. requests.Session -> httpx.Client
Automatically strips library prefix from paths so the agent can pass
either "httpx.get" or just "get" β both work correctly.
"""
clean_old_func = _strip_lib_prefix(old_lib, func_path)
clean_new_func = _strip_lib_prefix(new_lib, new_func_path) if new_func_path else clean_old_func
old_info = get_func_info(old_lib, clean_old_func)
new_info = get_func_info(new_lib, clean_new_func)
if not new_info["found"]:
new_info["note"] = f"'{clean_new_func}' not found in {new_lib} β may be a response method (not top-level), or renamed. Use it as response.{clean_new_func}() in your code."
return {
"old": old_info,
"new": new_info
}
def format_diff_for_agent(diff: dict[str, Any]) -> str:
"""Format the diff as clean text the LLM agent can reason about."""
old = diff["old"]
new = diff["new"]
lines = []
lines.append(f"## Migration: {old['lib']}.{old['func']} β {new['lib']}.{new['func']}")
lines.append(f"Versions β {old['lib']}: {old.get('version', '?')} | {new['lib']}: {new.get('version', '?')}")
lines.append("")
for label, info in [("Old", old), ("New", new)]:
source_label = {
"inspect": "β live inspect",
"not_found": "β not found"
}.get(info.get("source", ""), "?")
lines.append(f"### {label} ({info['lib']}.{info['func']}) [{source_label}]")
if not info["found"]:
lines.append(f" {info.get('error', 'Unknown error')}")
else:
lines.append(f" Signature: {info['signature']}")
lines.append(f" Docstring: {info['docstring']}")
lines.append("")
if "note" in new:
lines.append(f"### β Note\n{new['note']}")
return "\n".join(lines)
if __name__ == "__main__":
diff = get_func_diff("requests", "httpx", "get")
print(format_diff_for_agent(diff)) |