Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract with assignment for index assignment #396

Merged
merged 2 commits into from Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,7 +1,13 @@
# Release <unreleased>

# Syntax support

- #392 Add extract method refactoring of code containing `global` (@climbus)

## Bug fixes
- #391, #396 Extract method similar no longer replace the left-hand side of assignment



# Release 0.20.1

Expand All @@ -13,6 +19,7 @@ Date: 2021-09-18
instead of boolean



# Release 0.20.0

Date: 2021-09-18
Expand Down
2 changes: 1 addition & 1 deletion rope/refactor/extract.py
Expand Up @@ -312,7 +312,7 @@ def _find_matches(self, collector):

@staticmethod
def _is_assignment(region_match):
return isinstance(region_match.ast, ast.Attribute) and isinstance(region_match.ast.ctx, ast.Store)
return isinstance(region_match.ast, (ast.Attribute, ast.Subscript)) and isinstance(region_match.ast.ctx, ast.Store)

def _where_to_search(self):
if self.info.similar:
Expand Down
24 changes: 23 additions & 1 deletion ropetest/refactor/extracttest.py
Expand Up @@ -1811,7 +1811,7 @@ def second_method(someargs):

self.assertEqual(expected, refactored)

def test_extract_function_expression_with_assignment(self):
def test_extract_function_expression_with_assignment_to_attribute(self):
code = dedent('''\
class A(object):
def func(self):
Expand All @@ -1833,6 +1833,28 @@ def new_func(self):

self.assertEqual(expected, refactored)

def test_extract_function_expression_with_assignment_index(self):
code = dedent('''\
class A(object):
def func(self, val):
self[val] = 1
var_bb = self[val]
''')
extract_target = '= self[val]'
start, end = code.index(extract_target)+2, code.index(extract_target)+2 + len(extract_target) - 2
refactored = self.do_extract_method(code, start, end, 'new_func', similar=True)
expected = dedent('''\
class A(object):
def func(self, val):
self[val] = 1
var_bb = self.new_func(val)

def new_func(self, val):
return self[val]
''')

self.assertEqual(expected, refactored)

def test_extraction_method_with_global_variable(self):
code = dedent('''\
g = None
Expand Down