[Bug]: rename refactor leaves `from module import old_name` unchanged, so applied code raises ImportError
code-review-graph version
2.3.9 (installed from staging @ 6b12d11; same result with 2.3.9 from PyPI)
Operating system
macOS
Python version
3.12.14
AI platform
other (describe in "Additional context")
Output of code-review-graph status
Nodes: 5
Edges: 5
Files: 3
Languages: python
Last updated: 2026-10-09T20:04:09
Built on branch: main
Built at commit: 4984f9cda01e
Steps to reproduce
Minimal repo:
mkdir -p repro/pkg && cd repro && touch pkg/__init__.py
printf 'def helper(x):\n return x * 2\n' > pkg/util.py
printf 'from pkg.util import helper\n\n\ndef main():\n return [helper(i) for i in range(3)]\n' > pkg/core.py
git init -q && git add -A && git commit -qm init
code-review-graph build
Then rename helper to double and apply it. Over MCP (code-review-graph serve --repo .) that is refactor_tool {"mode":"rename","old_name":"helper","new_name":"double"} followed by apply_refactor_tool {"refactor_id":"<id>"}. Here is the same thing through the Python API:
python -c '
from code_review_graph.tools.refactor_tools import refactor_func, apply_refactor_func
p = refactor_func(mode="rename", old_name="helper", new_name="double", repo_root=".")
print([(e["file"], e["line"]) for e in p["edits"]])
print(apply_refactor_func(p["refactor_id"], repo_root="."))
'
python -c "import pkg.core"
Expected vs actual behavior
Expected: the preview includes an edit for the import line pkg/core.py:1, and the code still imports after the rename is applied.
Actual: the preview has only 2 edits, the definition (pkg/util.py:1) and the call (pkg/core.py:5). Applying returns "status": "ok", "edits_applied": 2, but pkg/core.py still says from pkg.util import helper:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File ".../repro/pkg/core.py", line 1, in <module>
from pkg.util import helper
ImportError: cannot import name 'helper' from 'pkg.util' (.../repro/pkg/util.py)
Additional context
AI platform: I called the server from a plain MCP stdio client (the Python mcp SDK), not from an editor integration. The Python API repro above shows the same result with no client involved.
Root cause: the "Import sites" block in rename_preview looks for IMPORTS_FROM edges whose target is the symbol's qualified name (.../pkg/util.py::helper):
https://github.com/tirth8205/code-review-graph/blob/6b12d11625cbec3b6773e076cb3d136464fa90e5/code_review_graph/refactor.py#L144-L157
For Python from X import name, the parser points the IMPORTS_FROM edge at the module file instead. Here is the edge from the repro graph:
IMPORTS_FROM | pkg/core.py | pkg/util.py | line 1
So that loop never matches a real Python import. The unit test fixture builds the edge as target="/repo/utils.py::helper", which is why the test passes:
https://github.com/tirth8205/code-review-graph/blob/6b12d11625cbec3b6773e076cb3d136464fa90e5/tests/test_refactor.py#L63-L67
A possible fix: also take IMPORTS_FROM edges whose target is node.file_path, and add an edit when that import line contains old_name as a whole word. A test would build the graph with the real parser and check that the renamed code still imports.
I'm happy to send a PR against staging for this if the approach sounds right.
Thanks for the fix, @tirth8205, and for going further than the report. I only flagged the missing from pkg.util import helper rewrite. Catching the first-substring replacement too (helper_count becoming double_count) means an applied rename can't quietly break an unrelated name. And covering relative imports and aliases makes it safe on real codebases, not just the minimal repro.