Skip to content

Commit a3b5529

Browse files
authored
rename: fix renaming toplevel names (#1260)
For toplevel module names imported via `import foo`, the rename codemod would fail to change these. This PR fixes that.
1 parent b04670c commit a3b5529

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

libcst/codemod/commands/rename.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def gen_replacement(self, original_name: str) -> str:
347347
module_as_name[0] + ".", module_as_name[1] + ".", 1
348348
)
349349

350-
if original_name == self.old_mod_or_obj:
350+
if self.old_module and original_name == self.old_mod_or_obj:
351351
return self.new_mod_or_obj
352352
elif original_name == self.old_name:
353353
return (

libcst/codemod/commands/tests/test_rename.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,25 @@ def test_import_parent_module_asname(self) -> None:
850850
z.c(z.c.d)
851851
"""
852852
self.assertCodemod(before, after, old_name="a.b.c", new_name="z.c")
853+
854+
def test_push_down_toplevel_names(self) -> None:
855+
before = """
856+
import foo
857+
foo.baz()
858+
"""
859+
after = """
860+
import quux.foo
861+
quux.foo.baz()
862+
"""
863+
self.assertCodemod(before, after, old_name="foo", new_name="quux.foo")
864+
865+
def test_push_down_toplevel_names_with_asname(self) -> None:
866+
before = """
867+
import foo as bar
868+
bar.baz()
869+
"""
870+
after = """
871+
import quux.foo
872+
quux.foo.baz()
873+
"""
874+
self.assertCodemod(before, after, old_name="foo", new_name="quux.foo")

0 commit comments

Comments
 (0)