scripts/tests/reproduction_utils/test_repath_import.py
scripts/tests/reproduction_utils/test_repath_import.pyBrowse 41 files
812 tokens
2,905 bytes
Token encoding: o200k_base
Snapshot a9fb1c3
← Back to SKILL.md
1import subprocess2import sys3from pathlib import Path4 5import pytest6 7sys.path.insert(0, str(Path(__file__).resolve().parents[2]))8 9import mechanical_refactor_reproduction_utils as rr10from mechanical_refactor_reproduction_utils import (11 Repro,12 _def_span,13 _find_class,14 _find_def,15 _replace_span,16 _slice_span,17 dedent,18 exec_command,19 git_add_and_commit,20 verify_mechanical_refactor,21)22from reproduction_testlib import _apply, _commit, _git, _write # noqa: F40123 24# --- repath_import / add_typechecking_import -----------------------------------25 26 27def test_repath_import_rewrites_nested_import(tmp_path: Path) -> None:28 """A function-scoped import is repathed in place; the bare call is untouched."""29 (tmp_path / "c.py").write_text(30 "class K:\n"31 " def run(self):\n"32 " from old.mod import foo\n"33 "\n"34 " return foo(1)\n"35 )36 r = Repro("b", "t").repath_import(37 "c.py", old_module="old.mod", new_module="new.mod", name="foo"38 )39 _apply(r, tmp_path)40 assert (tmp_path / "c.py").read_text() == (41 "class K:\n"42 " def run(self):\n"43 " from new.mod import foo\n"44 "\n"45 " return foo(1)\n"46 )47 48 49def test_repath_import_leaves_a_module_level_import(tmp_path: Path) -> None:50 """Only nested imports are repathed; a module-level import is left to the sorter."""51 (tmp_path / "c.py").write_text("from old.mod import foo\n\n\nx = foo(1)\n")52 r = Repro("b", "t").repath_import(53 "c.py", old_module="old.mod", new_module="new.mod", name="foo"54 )55 with pytest.raises(AssertionError):56 _apply(r, tmp_path)57 58 59def test_repath_import_repaths_a_multiline_aliased_nested_import(60 tmp_path: Path,61) -> None:62 """A nested multi-line from-import with an alias is repathed on its first line."""63 (tmp_path / "c.py").write_text(64 "def run():\n"65 " from old.mod import (\n"66 " foo as f,\n"67 " )\n"68 "\n"69 " return f(1)\n"70 )71 r = Repro("b", "t").repath_import(72 "c.py", old_module="old.mod", new_module="new.mod", name="foo"73 )74 _apply(r, tmp_path)75 assert (tmp_path / "c.py").read_text() == (76 "def run():\n"77 " from new.mod import (\n"78 " foo as f,\n"79 " )\n"80 "\n"81 " return f(1)\n"82 )83 84 85def test_repath_import_rewrites_a_relative_nested_import(tmp_path: Path) -> None:86 """A nested `from .mod import` matched by module name must actually be repathed."""87 (tmp_path / "c.py").write_text(88 "def run():\n from .mod import foo\n\n return foo(1)\n"89 )90 r = Repro("b", "t").repath_import(91 "c.py", old_module="mod", new_module="pkg.mod", name="foo"92 )93 _apply(r, tmp_path)94 assert "from pkg.mod import foo" in (tmp_path / "c.py").read_text()95