Skip to content

Commit

Permalink
Extract with assignment for index assignment
Browse files Browse the repository at this point in the history
Very similar to the case resolved in #391 by @climbus, but for index
assignment instead of attribute assignment.
  • Loading branch information
lieryan committed Sep 18, 2021
1 parent c3a4ac7 commit 69cda50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit 69cda50

Please sign in to comment.