Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# **Upcoming release**

- ...
- Fix changing constructor signatures for classes defined inside functions (#692).

# Release 1.15.0

Expand Down
5 changes: 4 additions & 1 deletion rope/refactor/change_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def _set_name_and_pyname(self):
and isinstance(pyobject.parent, pyobjects.PyClass)
):
pyclass = pyobject.parent
self.others = (pyclass.get_name(), pyclass.parent[pyclass.get_name()])
self.others = (
pyclass.get_name(),
pyclass.parent.get_scope()[pyclass.get_name()],
)

def _change_calls(
self,
Expand Down
92 changes: 92 additions & 0 deletions ropetest/refactor/change_signature_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,98 @@ def __init__(self, p):
self.mod.read(),
)

def test_changing_signature_for_function_local_constructor(self):
code = dedent("""\
def make():
class Local:
def __init__(self, value):
pass
item = Local(1)
item.__init__(2)
return item
""")
expected = dedent("""\
def make():
class Local:
def __init__(self):
pass
item = Local()
item.__init__()
return item
""")
offsets = (
code.index("Local"),
code.index("__init__"),
code.rindex("Local"),
code.rindex("__init__"),
)
for offset in offsets:
with self.subTest(offset=offset):
self.mod.write(code)
signature = change_signature.ChangeSignature(
self.project, self.mod, offset
)
self.project.do(
signature.get_changes([change_signature.ArgumentRemover(1)])
)
self.assertEqual(expected, self.mod.read())

def test_function_local_constructors_with_the_same_name_are_distinct(self):
self.mod.write(dedent("""\
def first():
class Local:
def __init__(self, value):
pass
return Local(1)
def second():
class Local:
def __init__(self, value):
pass
return Local(2)
"""))
signature = change_signature.ChangeSignature(
self.project, self.mod, self.mod.read().index("__init__")
)
self.project.do(signature.get_changes([change_signature.ArgumentRemover(1)]))
self.assertEqual(
dedent("""\
def first():
class Local:
def __init__(self):
pass
return Local()
def second():
class Local:
def __init__(self, value):
pass
return Local(2)
"""),
self.mod.read(),
)

def test_changing_signature_for_class_nested_constructor(self):
self.mod.write(dedent("""\
class Outer:
class Inner:
def __init__(self, value):
pass
item = Outer.Inner(1)
"""))
signature = change_signature.ChangeSignature(
self.project, self.mod, self.mod.read().index("__init__")
)
self.project.do(signature.get_changes([change_signature.ArgumentRemover(1)]))
self.assertEqual(
dedent("""\
class Outer:
class Inner:
def __init__(self):
pass
item = Outer.Inner()
"""),
self.mod.read(),
)

def test_redordering_arguments_reported_by_mft(self):
self.mod.write(dedent("""\
def f(a, b, c):
Expand Down
Loading