Skip to content

Commit

Permalink
Do not modify body of abstractmethods for fix-mutable-params
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Nov 17, 2023
1 parent f79d674 commit 4c55b12
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/core_codemods/fix_mutable_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def _build_new_body(self, new_var_decls, body):
new_body.extend(body[offset:])
return new_body

def _is_abstractmethod(self, node: cst.FunctionDef) -> bool:
for decorator in node.decorators:
match decorator.decorator:
case cst.Name("abstractmethod"):
return True

return False

def leave_FunctionDef(
self,
original_node: cst.FunctionDef,
Expand All @@ -151,7 +159,11 @@ def leave_FunctionDef(
new_var_decls,
add_annotation,
) = self._gather_and_update_params(original_node, updated_node)
new_body = self._build_new_body(new_var_decls, updated_node.body.body)
new_body = (
self._build_new_body(new_var_decls, updated_node.body.body)
if not self._is_abstractmethod(original_node)
else updated_node.body.body
)
if new_var_decls:
# If we're adding statements to the body, we know a change took place
self.add_change(original_node, self.CHANGE_DESCRIPTION)
Expand Down
19 changes: 19 additions & 0 deletions tests/codemods/test_fix_mutable_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,22 @@ def func(foo=None):
print(foo)
"""
self.run_and_assert(tmpdir, input_code, expected_output)

def test_dont_modify_abstractmethod_body(self, tmpdir):
input_code = """
from abc import ABC, abstractmethod
class Foo(ABC):
@abstractmethod
def foo(self, bar=[]):
pass
"""
expected_output = """
from abc import ABC, abstractmethod
class Foo(ABC):
@abstractmethod
def foo(self, bar=None):
pass
"""
self.run_and_assert(tmpdir, input_code, expected_output)

0 comments on commit 4c55b12

Please sign in to comment.