Skip to content

Commit

Permalink
rename: fix renaming toplevel names (#1260)
Browse files Browse the repository at this point in the history
For toplevel module names imported via `import foo`, the rename codemod would fail to change these. This PR fixes that.
  • Loading branch information
zsol authored Dec 11, 2024
1 parent b04670c commit a3b5529
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libcst/codemod/commands/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def gen_replacement(self, original_name: str) -> str:
module_as_name[0] + ".", module_as_name[1] + ".", 1
)

if original_name == self.old_mod_or_obj:
if self.old_module and original_name == self.old_mod_or_obj:
return self.new_mod_or_obj
elif original_name == self.old_name:
return (
Expand Down
22 changes: 22 additions & 0 deletions libcst/codemod/commands/tests/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,25 @@ def test_import_parent_module_asname(self) -> None:
z.c(z.c.d)
"""
self.assertCodemod(before, after, old_name="a.b.c", new_name="z.c")

def test_push_down_toplevel_names(self) -> None:
before = """
import foo
foo.baz()
"""
after = """
import quux.foo
quux.foo.baz()
"""
self.assertCodemod(before, after, old_name="foo", new_name="quux.foo")

def test_push_down_toplevel_names_with_asname(self) -> None:
before = """
import foo as bar
bar.baz()
"""
after = """
import quux.foo
quux.foo.baz()
"""
self.assertCodemod(before, after, old_name="foo", new_name="quux.foo")

0 comments on commit a3b5529

Please sign in to comment.