@@ -46,8 +46,6 @@ def external_dep():
4646
4747 # language=python
4848 EXPECTED_FILE_2_CONTENT = """
49- from file1 import external_dep
50-
5149def foo():
5250 return foo_dep() + 1
5351
@@ -68,7 +66,6 @@ def bar():
6866 return external_dep() + bar_dep()
6967"""
7068 # ===============================
71- # TODO: [low] Should maybe remove unused external_dep?
7269 # TODO: [low] Missing newline after import
7370
7471 with get_codebase_session (
@@ -84,7 +81,7 @@ def bar():
8481 file3 = codebase .get_file ("file3.py" )
8582
8683 bar = file2 .get_function ("bar" )
87- bar .move_to_file (file3 , include_dependencies = True , strategy = "update_all_imports" )
84+ bar .move_to_file (file3 , include_dependencies = True , strategy = "update_all_imports" , remove_unused_imports = True )
8885
8986 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
9087 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -171,7 +168,7 @@ def baz():
171168 file3 = codebase .get_file ("file3.py" )
172169
173170 bar_symbol = file2 .get_symbol ("bar" )
174- bar_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True )
171+ bar_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True , remove_unused_imports = True )
175172
176173 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
177174 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -266,7 +263,7 @@ def baz():
266263 file3 = codebase .get_file ("file3.py" )
267264
268265 bar_symbol = file2 .get_symbol ("bar" )
269- bar_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = False )
266+ bar_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = False , remove_unused_imports = True )
270267
271268 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
272269 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -321,8 +318,6 @@ def external_dep():
321318
322319 # language=python
323320 EXPECTED_FILE_2_CONTENT = """
324- from file1 import external_dep
325-
326321def foo():
327322 return foo_dep() + 1
328323
@@ -360,7 +355,7 @@ def bar():
360355 file3 = codebase .get_file ("file3.py" )
361356
362357 bar = file2 .get_function ("bar" )
363- bar .move_to_file (file3 , include_dependencies = True , strategy = "add_back_edge" )
358+ bar .move_to_file (file3 , include_dependencies = True , strategy = "add_back_edge" , remove_unused_imports = True )
364359
365360 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
366361 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -449,7 +444,7 @@ def baz():
449444 file3 = codebase .get_file ("file3.py" )
450445
451446 bar_symbol = file2 .get_symbol ("bar" )
452- bar_symbol .move_to_file (file1 , strategy = "add_back_edge" , include_dependencies = True )
447+ bar_symbol .move_to_file (file1 , strategy = "add_back_edge" , include_dependencies = True , remove_unused_imports = True )
453448
454449 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
455450 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -546,7 +541,7 @@ def baz():
546541 file3 = codebase .get_file ("file3.py" )
547542
548543 bar_symbol = file2 .get_symbol ("bar" )
549- bar_symbol .move_to_file (file1 , strategy = "add_back_edge" , include_dependencies = False )
544+ bar_symbol .move_to_file (file1 , strategy = "add_back_edge" , include_dependencies = False , remove_unused_imports = True )
550545
551546 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
552547 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -601,8 +596,6 @@ def external_dep():
601596
602597 # language=python
603598 EXPECTED_FILE_2_CONTENT = """
604- from file1 import external_dep
605-
606599def foo():
607600 return foo_dep() + 1
608601
@@ -638,7 +631,7 @@ def bar():
638631 file3 = codebase .get_file ("file3.py" )
639632
640633 bar = file2 .get_function ("bar" )
641- bar .move_to_file (file3 , include_dependencies = True , strategy = "duplicate_dependencies" )
634+ bar .move_to_file (file3 , include_dependencies = True , strategy = "duplicate_dependencies" , remove_unused_imports = True )
642635
643636 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
644637 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -732,7 +725,7 @@ def baz():
732725 file3 = codebase .get_file ("file3.py" )
733726
734727 bar_symbol = file2 .get_symbol ("bar" )
735- bar_symbol .move_to_file (file1 , strategy = "duplicate_dependencies" , include_dependencies = True )
728+ bar_symbol .move_to_file (file1 , strategy = "duplicate_dependencies" , include_dependencies = True , remove_unused_imports = True )
736729
737730 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
738731 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -833,7 +826,7 @@ def baz():
833826 file3 = codebase .get_file ("file3.py" )
834827
835828 bar_symbol = file2 .get_symbol ("bar" )
836- bar_symbol .move_to_file (file1 , strategy = "duplicate_dependencies" , include_dependencies = False )
829+ bar_symbol .move_to_file (file1 , strategy = "duplicate_dependencies" , include_dependencies = False , remove_unused_imports = True )
837830
838831 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
839832 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -873,13 +866,10 @@ def test_move_global_var(tmpdir) -> None:
873866
874867 # language=python
875868 EXPECTED_FILE_2_CONTENT = """
876- from import1 import thing1
877- from import2 import thing2, thing3
878869"""
879870
880871 # ===============================
881872 # TODO: [medium] Space messed up in file1
882- # TODO: [low] Dangling / unused import in file2
883873
884874 with get_codebase_session (
885875 tmpdir = tmpdir ,
@@ -892,7 +882,7 @@ def test_move_global_var(tmpdir) -> None:
892882 file2 = codebase .get_file ("file2.py" )
893883
894884 global_symbol = file2 .get_symbol ("GLOBAL" )
895- global_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True )
885+ global_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True , remove_unused_imports = True )
896886
897887 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
898888 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -957,7 +947,7 @@ def baz():
957947 file2 = codebase .get_file ("file2.py" )
958948
959949 bar_symbol = file2 .get_symbol ("bar" )
960- bar_symbol .move_to_file (file1 , strategy = "add_back_edge" , include_dependencies = True )
950+ bar_symbol .move_to_file (file1 , strategy = "add_back_edge" , include_dependencies = True , remove_unused_imports = True )
961951
962952 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
963953 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -1032,7 +1022,7 @@ def baz():
10321022
10331023 bar_func_symbol = file2 .get_symbol ("bar_func" )
10341024 assert bar_func_symbol
1035- bar_func_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True )
1025+ bar_func_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True , remove_unused_imports = True )
10361026
10371027 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
10381028 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -1091,7 +1081,6 @@ def bar_func():
10911081
10921082 # language=python
10931083 EXPECTED_FILE_2_CONTENT = """
1094- from app.file1 import foo_func
10951084from typing import Optional
10961085"""
10971086
@@ -1107,6 +1096,7 @@ def baz():
11071096
11081097 # ===============================
11091098 # TODO: [!HIGH!] Corrupted output in file3
1099+ # TODO: [medium] Self import of foo_func in file1
11101100 # TODO: [low] Unused imports in file2
11111101
11121102 with get_codebase_session (
@@ -1123,7 +1113,7 @@ def baz():
11231113
11241114 bar_func_symbol = file2 .get_symbol ("bar_func" )
11251115 assert bar_func_symbol
1126- bar_func_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True )
1116+ bar_func_symbol .move_to_file (file1 , strategy = "update_all_imports" , include_dependencies = True , remove_unused_imports = True )
11271117
11281118 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
11291119 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -1174,7 +1164,7 @@ def foo():
11741164 assert foo in bar .dependencies
11751165
11761166 file2 = codebase .create_file ("file2.py" , "" )
1177- foo .move_to_file (file2 , include_dependencies = True , strategy = "add_back_edge" )
1167+ foo .move_to_file (file2 , include_dependencies = True , strategy = "add_back_edge" , remove_unused_imports = True )
11781168
11791169 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
11801170 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
@@ -1268,8 +1258,8 @@ def external_dep2():
12681258
12691259 d1 = file1 .get_function ("external_dep" )
12701260 d2 = file1 .get_function ("external_dep2" )
1271- d1 .move_to_file (file3 , include_dependencies = True , strategy = "update_all_imports" )
1272- d2 .move_to_file (file4 , include_dependencies = True , strategy = "update_all_imports" )
1261+ d1 .move_to_file (file3 , include_dependencies = True , strategy = "update_all_imports" , remove_unused_imports = True )
1262+ d2 .move_to_file (file4 , include_dependencies = True , strategy = "update_all_imports" , remove_unused_imports = True )
12731263
12741264 assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
12751265 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
0 commit comments