Skip to content

Commit

Permalink
rename: don't eat commas unnecessarily (#1256)
Browse files Browse the repository at this point in the history
#1254 was a bit too aggressive in removing commas. They shouldn't be removed if there are parenthesis around the imported names.
  • Loading branch information
zsol authored Dec 2, 2024
1 parent 8c30fce commit d24192a
Show file tree
Hide file tree
Showing 2 changed files with 33 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 @@ -252,7 +252,7 @@ def leave_ImportFrom(
# This import might be in use elsewhere in the code, so schedule a potential removal.
self.scheduled_removals.add(original_node)
new_names.append(import_alias)
if isinstance(new_names[-1].comma, cst.Comma):
if isinstance(new_names[-1].comma, cst.Comma) and updated_node.rpar is None:
new_names[-1] = new_names[-1].with_changes(
comma=cst.MaybeSentinel.DEFAULT
)
Expand Down
32 changes: 32 additions & 0 deletions libcst/codemod/commands/tests/test_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,38 @@ class Foo(b):
new_name="f.d",
)

def test_comma_import_from_parens(self) -> None:
before = """
from a import (
b,
c,
d,
)
from x import (y,)
class Foo(b):
bar: c.bar
baz: d.baz
"""
after = """
from a import (
b,
c,
)
from x import (y,)
from f import d
class Foo(b):
bar: c.bar
baz: d.baz
"""
self.assertCodemod(
before,
after,
old_name="a.d",
new_name="f.d",
)

def test_no_removal_of_import_in_use(self) -> None:
before = """
import a
Expand Down

0 comments on commit d24192a

Please sign in to comment.